From 8d654a6b4273f293b8925c2796491abbb785761d Mon Sep 17 00:00:00 2001 From: Erik Date: Tue, 26 Jan 2021 12:07:39 -0800 Subject: [PATCH] Fix Content-Type header parsing in 'juniper_actix' (#863, #860) --- juniper_actix/src/lib.rs | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/juniper_actix/src/lib.rs b/juniper_actix/src/lib.rs index e62ded6d..23f4cc05 100644 --- a/juniper_actix/src/lib.rs +++ b/juniper_actix/src/lib.rs @@ -42,8 +42,8 @@ Check the LICENSE file for details. use actix_web::{ error::{ErrorBadRequest, ErrorMethodNotAllowed, ErrorUnsupportedMediaType}, - http::{header::CONTENT_TYPE, Method}, - web, Error, FromRequest, HttpRequest, HttpResponse, + http::Method, + web, Error, FromRequest, HttpMessage, HttpRequest, HttpResponse, }; use juniper::{ http::{ @@ -152,16 +152,12 @@ where CtxT: Sync, S: ScalarValue + Send + Sync, { - let content_type_header = req - .headers() - .get(CONTENT_TYPE) - .and_then(|hv| hv.to_str().ok()); - let req = match content_type_header { - Some("application/json") => { + let req = match req.content_type() { + "application/json" => { let body = String::from_request(&req, &mut payload.into_inner()).await?; serde_json::from_str::>(&body).map_err(ErrorBadRequest) } - Some("application/graphql") => { + "application/graphql" => { let body = String::from_request(&req, &mut payload.into_inner()).await?; Ok(GraphQLBatchRequest::Single(GraphQLRequest::new( body, None, None, @@ -609,7 +605,7 @@ mod tests { ); let req = test::TestRequest::post() - .header("content-type", "application/json") + .header("content-type", "application/json; charset=utf-8") .set_payload( r##"{ "variables": null, "query": "{ hero(episode: NEW_HOPE) { name } }" }"##, )