@@ -6,7 +6,7 @@ use axum::{
6
6
async_trait,
7
7
body:: Body ,
8
8
extract:: { FromRequest , FromRequestParts , Query } ,
9
- http:: { HeaderValue , Method , Request , StatusCode } ,
9
+ http:: { header , HeaderValue , Method , Request , StatusCode } ,
10
10
response:: { IntoResponse as _, Response } ,
11
11
Json , RequestExt as _,
12
12
} ;
85
85
async fn from_request ( mut req : Request < Body > , state : & State ) -> Result < Self , Self :: Rejection > {
86
86
let content_type = req
87
87
. headers ( )
88
- . get ( "content-type" )
88
+ . get ( header :: CONTENT_TYPE )
89
89
. map ( HeaderValue :: to_str)
90
90
. transpose ( )
91
91
. map_err ( |_| {
@@ -122,22 +122,24 @@ where
122
122
. into_response ( )
123
123
} )
124
124
} ) ,
125
- ( & Method :: POST , Some ( "application/json" ) ) => {
125
+ ( & Method :: POST , Some ( x ) ) if x . starts_with ( "application/json" ) => {
126
126
Json :: < GraphQLBatchRequest < S > > :: from_request ( req, state)
127
127
. await
128
128
. map ( |req| Self ( req. 0 ) )
129
129
. map_err ( |e| {
130
130
( StatusCode :: BAD_REQUEST , format ! ( "Invalid JSON body: {e}" ) ) . into_response ( )
131
131
} )
132
132
}
133
- ( & Method :: POST , Some ( "application/graphql" ) ) => String :: from_request ( req, state)
134
- . await
135
- . map ( |body| {
136
- Self ( GraphQLBatchRequest :: Single ( GraphQLRequest :: new (
137
- body, None , None ,
138
- ) ) )
139
- } )
140
- . map_err ( |_| ( StatusCode :: BAD_REQUEST , "Not valid UTF-8 body" ) . into_response ( ) ) ,
133
+ ( & Method :: POST , Some ( x) ) if x. starts_with ( "application/graphql" ) => {
134
+ String :: from_request ( req, state)
135
+ . await
136
+ . map ( |body| {
137
+ Self ( GraphQLBatchRequest :: Single ( GraphQLRequest :: new (
138
+ body, None , None ,
139
+ ) ) )
140
+ } )
141
+ . map_err ( |_| ( StatusCode :: BAD_REQUEST , "Not valid UTF-8 body" ) . into_response ( ) )
142
+ }
141
143
( & Method :: POST , _) => Err ( (
142
144
StatusCode :: UNSUPPORTED_MEDIA_TYPE ,
143
145
"`Content-Type` header is expected to be either `application/json` or \
@@ -246,6 +248,22 @@ mod juniper_request_tests {
246
248
assert_eq ! ( do_from_request( req) . await , expected) ;
247
249
}
248
250
251
+ #[ tokio:: test]
252
+ async fn from_json_post_request_with_charset ( ) {
253
+ let req = Request :: post ( "/" )
254
+ . header ( "content-type" , "application/json; charset=utf-8" )
255
+ . body ( Body :: from ( r#"{"query": "{ add(a: 2, b: 3) }"}"# ) )
256
+ . unwrap_or_else ( |e| panic ! ( "cannot build `Request`: {e}" ) ) ;
257
+
258
+ let expected = JuniperRequest ( GraphQLBatchRequest :: Single ( GraphQLRequest :: new (
259
+ "{ add(a: 2, b: 3) }" . to_string ( ) ,
260
+ None ,
261
+ None ,
262
+ ) ) ) ;
263
+
264
+ assert_eq ! ( do_from_request( req) . await , expected) ;
265
+ }
266
+
249
267
#[ tokio:: test]
250
268
async fn from_graphql_post_request ( ) {
251
269
let req = Request :: post ( "/" )
@@ -262,6 +280,22 @@ mod juniper_request_tests {
262
280
assert_eq ! ( do_from_request( req) . await , expected) ;
263
281
}
264
282
283
+ #[ tokio:: test]
284
+ async fn from_graphql_post_request_with_charset ( ) {
285
+ let req = Request :: post ( "/" )
286
+ . header ( "content-type" , "application/graphql; charset=utf-8" )
287
+ . body ( Body :: from ( r#"{ add(a: 2, b: 3) }"# ) )
288
+ . unwrap_or_else ( |e| panic ! ( "cannot build `Request`: {e}" ) ) ;
289
+
290
+ let expected = JuniperRequest ( GraphQLBatchRequest :: Single ( GraphQLRequest :: new (
291
+ "{ add(a: 2, b: 3) }" . to_string ( ) ,
292
+ None ,
293
+ None ,
294
+ ) ) ) ;
295
+
296
+ assert_eq ! ( do_from_request( req) . await , expected) ;
297
+ }
298
+
265
299
/// Performs [`JuniperRequest::from_request()`].
266
300
async fn do_from_request ( req : Request < Body > ) -> JuniperRequest {
267
301
match JuniperRequest :: from_request ( req, & ( ) ) . await {
0 commit comments