From 420b17d985c5cf7af5bc33d11fd18f7e389ef014 Mon Sep 17 00:00:00 2001 From: Ozgur Akkurt Date: Tue, 5 Oct 2021 03:34:42 +0300 Subject: [PATCH] add awc::ClientResponse::json_with_content_type_unchecked --- awc/src/response.rs | 33 +++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/awc/src/response.rs b/awc/src/response.rs index a966edd08..37bf8bd75 100644 --- a/awc/src/response.rs +++ b/awc/src/response.rs @@ -203,15 +203,25 @@ where MessageBody::new(self) } - /// Loads and parse `application/json` encoded body. - /// Return `JsonBody` future. It resolves to a `T` value. + /// Loads and parses `application/json` encoded body. + /// Returns a s`JsonBody` future. It resolves to a `T` value. /// /// Returns error: /// /// * content type is not `application/json` /// * content length is greater than 256k pub fn json(&mut self) -> JsonBody { - JsonBody::new(self) + JsonBody::new(self, true) + } + + /// Loads and parses `application/json` encoded body. + /// Returns a `JsonBody` future. It resolves to a `T` value. + /// + /// Returns error: + /// + /// * content length is greater than 256k + pub fn json_with_content_type_unchecked(&mut self) -> JsonBody { + JsonBody::new(self, false) } } @@ -337,14 +347,17 @@ where U: DeserializeOwned, { /// Create `JsonBody` for request. - pub fn new(res: &mut ClientResponse) -> Self { - // check content-type - let json = if let Ok(Some(mime)) = res.mime_type() { - mime.subtype() == mime::JSON || mime.suffix() == Some(mime::JSON) - } else { - false + pub fn new(res: &mut ClientResponse, check_content_type: bool) -> Self { + // Create a function to check content-type + let json = || { + if let Ok(Some(mime)) = res.mime_type() { + mime.subtype() == mime::JSON || mime.suffix() == Some(mime::JSON) + } else { + false + } }; - if !json { + + if check_content_type && !json() { return JsonBody { length: None, fut: None,