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

This commit is contained in:
Lucas Fernandes Nogueira
2020-06-19 14:59:16 -03:00
committed by GitHub
parent 7e790ac0a6
commit e0c330cfb6
2 changed files with 15 additions and 12 deletions

View File

@@ -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()?)?,
};

View File

@@ -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,
},
)