Set a default content type for JSON requests

When sending a JSON request a Content-Type of application/json is
usually wanted. This is often set as a default for JSON methods by HTTP
clients so can be confusing when it is not set. However, we do not want
to prevent the user from setting their own Content-Type.
This commit is contained in:
Rob Young
2020-02-05 12:00:35 +00:00
committed by Martin Algesten
parent fb158ca73a
commit 28bdb89175
2 changed files with 39 additions and 0 deletions

View File

@@ -132,6 +132,9 @@ impl Request {
/// ```
#[cfg(feature = "json")]
pub fn send_json(&mut self, data: SerdeValue) -> Response {
if let None = self.header("Content-Type") {
self.set("Content-Type", "application/json");
}
self.do_call(Payload::JSON(data))
}