Skip to content

Commit

Permalink
send_json to set content-type
Browse files Browse the repository at this point in the history
  • Loading branch information
algesten committed Oct 15, 2024
1 parent e2ae06c commit 95694c4
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,8 @@ impl RequestBuilder<WithBody> {
/// Requires the **json** feature.
///
/// The data typically derives [`Serialize`](serde::Serialize) and is converted
/// to a string before sending (does allocate).
/// to a string before sending (does allocate). Will set the content-type header
/// `application/json`.
///
/// ```
/// use serde::Serialize;
Expand All @@ -399,8 +400,16 @@ impl RequestBuilder<WithBody> {
/// ```
#[cfg(feature = "json")]
pub fn send_json(self, data: impl serde::ser::Serialize) -> Result<Response<Body>, Error> {
let request = self.builder.body(())?;
let mut request = self.builder.body(())?;
let body = SendBody::from_json(&data)?;

if !request.headers().has_content_type() {
request.headers_mut().append(
http::header::CONTENT_TYPE,
HeaderValue::from_static("application/json; charset=utf-8"),
);
}

do_call(self.agent, request, self.query_extra, body)
}
}
Expand Down

0 comments on commit 95694c4

Please sign in to comment.