Przeglądaj źródła

fix(tauri) httpRequest API JSON/Text response reader, header value as string, closes #691 (#702)

Lucas Fernandes Nogueira 5 lat temu
rodzic
commit
e0c330cfb6
2 zmienionych plików z 15 dodań i 12 usunięć
  1. 11 11
      tauri-api/src/http.rs
  2. 4 1
      tauri/src/endpoints/http.rs

+ 11 - 11
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<HashMap<String, Value>>,
+  pub params: Option<HashMap<String, String>>,
   /// The request headers
-  pub headers: Option<HashMap<String, Value>>,
+  pub headers: Option<HashMap<String, String>>,
   /// The request body
   pub body: Option<Value>,
   /// 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<HashMap<String, Value>>,
+  pub params: Option<HashMap<String, String>>,
   /// The request headers
-  pub headers: Option<HashMap<String, Value>>,
+  pub headers: Option<HashMap<String, String>>,
   /// The request body
   pub body: Option<Value>,
   /// Whether to follow redirects or not
@@ -132,12 +132,12 @@ impl HttpRequestBuilder {
     }
   }
 
-  pub fn params(mut self, params: HashMap<String, Value>) -> Self {
+  pub fn params(mut self, params: HashMap<String, String>) -> Self {
     self.params = Some(params);
     self
   }
 
-  pub fn headers(mut self, headers: HashMap<String, Value>) -> Self {
+  pub fn headers(mut self, headers: HashMap<String, String>) -> Self {
     self.headers = Some(headers);
     self
   }
@@ -221,10 +221,7 @@ pub fn make_request(options: HttpRequestOptions) -> crate::Result<String> {
 
   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<String> {
   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::<Value>()?;
+        serde_json::to_string(&result)?
+      }
       ResponseType::Text => response.text()?,
       ResponseType::Binary => serde_json::to_string(&response.bytes()?)?,
     };

+ 4 - 1
tauri/src/endpoints/http.rs

@@ -14,7 +14,10 @@ pub fn make_request<T: 'static>(
       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,
         },
       )