Skip to content

Commit b13ca4b

Browse files
authored
bug: fix custom content-type overidden by json method (#1833)
* bug: fix custom content-type overided for json method * fix format --------- Co-authored-by: hulin <hulin2021@gmail.com>
1 parent eca2a2f commit b13ca4b

File tree

4 files changed

+76
-4
lines changed

4 files changed

+76
-4
lines changed

src/async_impl/request.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -440,8 +440,10 @@ impl RequestBuilder {
440440
if let Ok(ref mut req) = self.request {
441441
match serde_json::to_vec(json) {
442442
Ok(body) => {
443-
req.headers_mut()
444-
.insert(CONTENT_TYPE, HeaderValue::from_static("application/json"));
443+
if !req.headers().contains_key(CONTENT_TYPE) {
444+
req.headers_mut()
445+
.insert(CONTENT_TYPE, HeaderValue::from_static("application/json"));
446+
}
445447
*req.body_mut() = Some(body.into());
446448
}
447449
Err(err) => error = Some(crate::error::builder(err)),

src/blocking/request.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -494,8 +494,10 @@ impl RequestBuilder {
494494
if let Ok(ref mut req) = self.request {
495495
match serde_json::to_vec(json) {
496496
Ok(body) => {
497-
req.headers_mut()
498-
.insert(CONTENT_TYPE, HeaderValue::from_static("application/json"));
497+
if !req.headers().contains_key(CONTENT_TYPE) {
498+
req.headers_mut()
499+
.insert(CONTENT_TYPE, HeaderValue::from_static("application/json"));
500+
}
499501
*req.body_mut() = Some(body.into());
500502
}
501503
Err(err) => error = Some(crate::error::builder(err)),

tests/blocking.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
mod support;
2+
3+
use http::header::CONTENT_TYPE;
4+
use http::HeaderValue;
5+
use std::collections::HashMap;
26
use support::*;
37

48
#[test]
@@ -328,3 +332,33 @@ fn test_body_from_bytes() {
328332

329333
assert_eq!(request.body().unwrap().as_bytes(), Some(body.as_bytes()));
330334
}
335+
336+
#[test]
337+
#[cfg(feature = "json")]
338+
fn blocking_add_json_default_content_type_if_not_set_manually() {
339+
let mut map = HashMap::new();
340+
map.insert("body", "json");
341+
let content_type = HeaderValue::from_static("application/vnd.api+json");
342+
let req = reqwest::blocking::Client::new()
343+
.post("https://google.com/")
344+
.header(CONTENT_TYPE, &content_type)
345+
.json(&map)
346+
.build()
347+
.expect("request is not valid");
348+
349+
assert_eq!(content_type, req.headers().get(CONTENT_TYPE).unwrap());
350+
}
351+
352+
#[test]
353+
#[cfg(feature = "json")]
354+
fn blocking_update_json_content_type_if_set_manually() {
355+
let mut map = HashMap::new();
356+
map.insert("body", "json");
357+
let req = reqwest::blocking::Client::new()
358+
.post("https://google.com/")
359+
.json(&map)
360+
.build()
361+
.expect("request is not valid");
362+
363+
assert_eq!("application/json", req.headers().get(CONTENT_TYPE).unwrap());
364+
}

tests/client.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
#![cfg(not(target_arch = "wasm32"))]
22
mod support;
3+
34
use futures_util::stream::StreamExt;
5+
use http::header::CONTENT_TYPE;
6+
use http::HeaderValue;
7+
use std::collections::HashMap;
48
use support::*;
59

610
use reqwest::Client;
@@ -372,3 +376,33 @@ async fn test_allowed_methods() {
372376

373377
assert!(resp.is_err());
374378
}
379+
380+
#[test]
381+
#[cfg(feature = "json")]
382+
fn add_json_default_content_type_if_not_set_manually() {
383+
let mut map = HashMap::new();
384+
map.insert("body", "json");
385+
let content_type = HeaderValue::from_static("application/vnd.api+json");
386+
let req = Client::new()
387+
.post("https://google.com/")
388+
.header(CONTENT_TYPE, &content_type)
389+
.json(&map)
390+
.build()
391+
.expect("request is not valid");
392+
393+
assert_eq!(content_type, req.headers().get(CONTENT_TYPE).unwrap());
394+
}
395+
396+
#[test]
397+
#[cfg(feature = "json")]
398+
fn update_json_content_type_if_set_manually() {
399+
let mut map = HashMap::new();
400+
map.insert("body", "json");
401+
let req = Client::new()
402+
.post("https://google.com/")
403+
.json(&map)
404+
.build()
405+
.expect("request is not valid");
406+
407+
assert_eq!("application/json", req.headers().get(CONTENT_TYPE).unwrap());
408+
}

0 commit comments

Comments
 (0)