diff --git a/tauri-api/src/http.rs b/tauri-api/src/http.rs index f44522643..88b3e4d3c 100644 --- a/tauri-api/src/http.rs +++ b/tauri-api/src/http.rs @@ -43,9 +43,9 @@ pub struct HttpRequestOptions { /// The request URL pub url: String, /// The request query params - pub params: Option>, + pub params: Option>, /// The request headers - pub headers: Option>, + pub headers: Option>, /// The request body pub body: Option, /// Whether to follow redirects or not @@ -90,9 +90,9 @@ pub struct HttpRequestBuilder { /// The request URL pub url: String, /// The request query params - pub params: Option>, + pub params: Option>, /// The request headers - pub headers: Option>, + pub headers: Option>, /// The request body pub body: Option, /// Whether to follow redirects or not @@ -132,12 +132,12 @@ impl HttpRequestBuilder { } } - pub fn params(mut self, params: HashMap) -> Self { + pub fn params(mut self, params: HashMap) -> Self { self.params = Some(params); self } - pub fn headers(mut self, headers: HashMap) -> Self { + pub fn headers(mut self, headers: HashMap) -> Self { self.headers = Some(headers); self } @@ -221,10 +221,7 @@ pub fn make_request(options: HttpRequestOptions) -> crate::Result { if let Some(headers) = options.headers { for (header, header_value) in headers.iter() { - builder = builder.header( - HeaderName::from_bytes(header.as_bytes())?, - format!("{}", header_value), - ); + builder = builder.header(HeaderName::from_bytes(header.as_bytes())?, header_value); } } @@ -283,7 +280,10 @@ pub fn make_request(options: HttpRequestOptions) -> crate::Result { let response = response?; if response.is_success() { let response_data = match options.response_type.unwrap_or(ResponseType::Json) { - ResponseType::Json => response.json()?, + ResponseType::Json => { + let result = response.json::()?; + serde_json::to_string(&result)? + } ResponseType::Text => response.text()?, ResponseType::Binary => serde_json::to_string(&response.bytes()?)?, }; diff --git a/tauri/src/endpoints/http.rs b/tauri/src/endpoints/http.rs index 35e99ea1f..a97a650cf 100644 --- a/tauri/src/endpoints/http.rs +++ b/tauri/src/endpoints/http.rs @@ -14,7 +14,10 @@ pub fn make_request( let response_type = options.response_type.clone(); request(options).map( |response| match response_type.unwrap_or(ResponseType::Json) { - ResponseType::Text => format!(r#""{}""#, response), + ResponseType::Text => format!( + r#""{}""#, + response.replace(r#"""#, r#"\""#).replace(r#"\\""#, r#"\""#) + ), _ => response, }, )