fix: fallback to plaintext if json parse fails
This commit is contained in:
parent
c29c91a7b9
commit
94fcb8efdc
1 changed files with 13 additions and 5 deletions
|
@ -209,16 +209,24 @@ fn replace_recursive(element: toml::Value, env: &toml::Table) -> Result<toml::Va
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn format_body(res: reqwest::Response) -> Result<String, PostWomanError> {
|
async fn format_body(res: reqwest::Response) -> Result<String, PostWomanError> {
|
||||||
match res.headers().get("Content-Type") {
|
let content_type = res.headers().get("Content-Type").cloned();
|
||||||
None => Ok(res.text().await?),
|
let raw = res.bytes().await?;
|
||||||
|
match content_type {
|
||||||
|
None => Ok(String::from_utf8_lossy(&raw).to_string()),
|
||||||
Some(v) => {
|
Some(v) => {
|
||||||
let content_type = v.to_str()?;
|
let content_type = v.to_str()?;
|
||||||
if content_type.starts_with("application/json") {
|
if content_type.starts_with("application/json") {
|
||||||
Ok(serde_json::to_string_pretty(&res.json::<serde_json::Value>().await?)?)
|
match serde_json::from_slice::<serde_json::Value>(&raw) {
|
||||||
|
Ok(x) => Ok(serde_json::to_string_pretty(&x)?),
|
||||||
|
Err(e) => {
|
||||||
|
eprintln!(" ? content-type is 'json' but content is not valid json: {e}");
|
||||||
|
Ok(String::from_utf8_lossy(&raw).to_string())
|
||||||
|
},
|
||||||
|
}
|
||||||
} else if content_type.starts_with("text/plain") || content_type.starts_with("text/html") {
|
} else if content_type.starts_with("text/plain") || content_type.starts_with("text/html") {
|
||||||
Ok(res.text().await?)
|
Ok(String::from_utf8_lossy(&raw).to_string())
|
||||||
} else {
|
} else {
|
||||||
Ok(format!("base64({})\n", BASE64_STANDARD.encode(res.bytes().await?)))
|
Ok(format!("base64({})\n", BASE64_STANDARD.encode(raw)))
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue