feat: format json if content-type json, show body
This commit is contained in:
parent
2f8920eefe
commit
b1ab7f433a
3 changed files with 16 additions and 2 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
@ -813,6 +813,7 @@ checksum = "26072860ba924cbfa98ea39c8c19b4dd6a4a25423dbdf219c1eca91aa0cf6964"
|
|||
name = "postwoman"
|
||||
version = "0.2.0"
|
||||
dependencies = [
|
||||
"base64",
|
||||
"chrono",
|
||||
"clap",
|
||||
"http",
|
||||
|
|
|
@ -6,6 +6,7 @@ edition = "2021"
|
|||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
base64 = "0.22.1"
|
||||
chrono = "0.4"
|
||||
clap = { version = "4.5", features = ["derive"] }
|
||||
http = "1.1.0"
|
||||
|
|
16
src/model.rs
16
src/model.rs
|
@ -1,5 +1,6 @@
|
|||
use std::str::FromStr;
|
||||
|
||||
use base64::{prelude::BASE64_STANDARD, Engine};
|
||||
use reqwest::header::{HeaderMap, HeaderName, HeaderValue};
|
||||
|
||||
use crate::APP_USER_AGENT;
|
||||
|
@ -151,8 +152,8 @@ impl Endpoint {
|
|||
Ok(match self.extract.unwrap_or_default() {
|
||||
StringOr::Str(_query) => todo!(),
|
||||
StringOr::T(Extractor::Discard) => "".to_string(),
|
||||
StringOr::T(Extractor::Debug) => format!("{res:#?}\n"),
|
||||
StringOr::T(Extractor::Body) => res.text().await? + "\n",
|
||||
StringOr::T(Extractor::Debug) => format!("{res:#?}\nBody: ") + &format_body(res).await? + "\n", // ughhh
|
||||
StringOr::T(Extractor::Body) => format_body(res).await?,
|
||||
StringOr::T(Extractor::Header { key }) => res
|
||||
.headers()
|
||||
.get(&key)
|
||||
|
@ -164,6 +165,17 @@ impl Endpoint {
|
|||
}
|
||||
}
|
||||
|
||||
async fn format_body(res: reqwest::Response) -> Result<String, PostWomanError> {
|
||||
match res.headers().get("Content-Type") {
|
||||
None => Ok(res.text().await? + "\n"),
|
||||
Some(v) => match v.to_str()? {
|
||||
"application/json" => Ok(serde_json::to_string_pretty(&res.json::<serde_json::Value>().await?)? + "\n"),
|
||||
"text/plain" => Ok(res.text().await? + "\n"),
|
||||
_ => Ok(format!("base64({})\n", BASE64_STANDARD.encode(res.bytes().await?))),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
|
||||
#[serde(untagged)]
|
||||
pub enum StringOr<T> {
|
||||
|
|
Loading…
Reference in a new issue