poem icon indicating copy to clipboard operation
poem copied to clipboard

charset=utf-8 for application/x-www-form-urlencoded causes HTTP response 415

Open dreamerlzl opened this issue 2 years ago • 1 comments

question

when I am using HTTPie to test an endpoint with http -f POST http://localhost:8081/subscription name=foo, the following error is reported

HTTP/1.1 415 Unsupported Media Type
content-length: 116
date: Wed, 21 Sep 2022 03:19:31 GMT

invalid content type `application/x-www-form-urlencoded; charset=utf-8`, expect: `application/x-www-form-urlencoded`

(httpie will automatically add charset=utf-8 in a form POST request) Is this an expected behavior?

version

poem = { version = "1.3.40", features = ["test"] }
poem-openapi = { version = "2.0.11", features = ["swagger-ui"] }

my test code

my source code is here: https://github.com/dreamerlzl/zero2prod/blob/main/src/routes/subscribe.rs

dreamerlzl avatar Sep 21 '22 03:09 dreamerlzl

I haven't figured out a solution to this problem, so it can only be application/x-www-form-urlencoded. 😂

sunli829 avatar Sep 21 '22 03:09 sunli829

I'm getting this error too, even without the charset=utf8 part, and unfortunately I don't have control over my clients here. Is there some way to work around this?

the `Content-Type` requested by the client is not supported: application/x-www-form-urlencoded

banool avatar Sep 26 '22 16:09 banool

Okay I updated Poem and the error randomly disappeared 🤔. I can't see a change that would fix this but I'll accept it lol.

banool avatar Sep 26 '22 16:09 banool

@sunli829 Do you think it's appropriate to update poem's code to use mime's essence_str to decide the content-type of the request? Thus the charset parameter should not be a problem. If it's ok, I'd be happy to shoot a PR.

brunobell avatar Oct 18 '22 03:10 brunobell

I've fixed this, please help me test it. 🙂

sunli829 avatar Oct 18 '22 10:10 sunli829

I've fixed this, please help me test it. slightly_smiling_face

update: the following error occurs because in my Cargo.toml

poem = { version = "1.3.40", features = ["test"] }
poem-openapi = { git = "https://github.com/poem-web/poem", rev="a43e94f2cfbefdbb484e3bdb51fafdb05201dbbb", features = ["swagger-ui"] }

after changing it to

poem = { git = "https://github.com/poem-web/poem", branch="master", features = ["testi"] }
poem-openapi = { git = "https://github.com/poem-web/poem", branch="master", features = ["swagger-ui"] }

the error disappears; and the fix works! no more 415.


Thank you! But after setting

poem-openapi = { git = "https://github.com/poem-web/poem", rev="a43e94f2cfbefdbb484e3bdb51fafdb05201dbbb", features = ["swagger-ui"] }

my project doesn't compile, saying that

error[E0277]: the trait bound `OpenApiService<SubscribeApi, ()>: poem::Endpoint` is not satisfied
  --> src/routes/mod.rs:28:19
   |
28 |     route = route.nest("/", api_service).nest("/docs", ui);
   |                   ^^^^ the trait `poem::Endpoint` is not implemented for `OpenApiService<SubscribeApi, ()>`
   |
   = help: the following other types implement trait `poem::Endpoint`:
             &'impl0 T
             Arc<T>
             health_check
             poem::Route
             poem::RouteDomain
             poem::RouteMethod
             poem::RouteScheme
             poem::endpoint::After<E, F>
           and 22 others
   = note: required for `OpenApiService<SubscribeApi, ()>` to implement `poem::IntoEndpoint`

error[E0277]: the trait bound `impl poem::endpoint::endpoint::Endpoint: poem::Endpoint` is not satisfied
  --> src/routes/subscribe.rs:21:41
   |
21 | ) -> (OpenApiService<SubscribeApi, ()>, impl Endpoint) {
   |                                         ^^^^^^^^^^^^^ the trait `poem::Endpoint` is not implemented for `impl poem::endpoint::endpoint::Endpoint`
...
25 |     (api_service, ui)
   |     ----------------- return type was inferred to be `(OpenApiService<SubscribeApi, ()>, impl poem::endpoint::endpoint::Endpoint)` here
   |
   = help: the following other types implement trait `poem::Endpoint`:
             &'impl0 T
             Arc<T>
             health_check
             poem::Route
             poem::RouteDomain
             poem::RouteMethod
             poem::RouteScheme
             poem::endpoint::After<E, F>
           and 22 others

error[E0277]: the trait bound `impl poem::endpoint::endpoint::Endpoint<Output = poem::response::Response>: poem::Endpoint` is not satisfied
  --> src/routes/subscribe.rs:34:1
   |
34 | #[OpenApi]
   | ^^^^^^^^^^ the trait `poem::Endpoint` is not implemented for `impl poem::endpoint::endpoint::Endpoint<Output = poem::response::Response>`
...
37 |     #[oai(path = "/subscription", method = "post", transform = "add_tracing")]
   |                                                                ------------- required by a bound introduced by this call
   |
   = help: the following other types implement trait `poem::Endpoint`:
             &'impl0 T
             Arc<T>
             health_check
             poem::Route
             poem::RouteDomain
             poem::RouteMethod
             poem::RouteScheme
             poem::endpoint::After<E, F>
           and 22 others
note: required by a bound in `add_tracing`
  --> src/routes/mod.rs:32:25
   |
32 | fn add_tracing(ep: impl Endpoint) -> impl Endpoint {
   |                         ^^^^^^^^ required by this bound in `add_tracing`

error[E0277]: the trait bound `impl poem::Endpoint: poem::endpoint::endpoint::Endpoint` is not satisfied
   --> src/routes/subscribe.rs:34:1
    |
34  | #[OpenApi]
    | ^^^^^^^^^^ the trait `poem::endpoint::endpoint::Endpoint` is not implemented for `impl poem::Endpoint`
    |
    = help: the following other types implement trait `poem::endpoint::endpoint::Endpoint`:
              &'impl0 T
              Arc<T>
              CookieJarManagerEndpoint<E>
              poem::endpoint::after::After<E, F>
              poem::endpoint::and_then::AndThen<E, F>
              poem::endpoint::around::Around<E, F>
              poem::endpoint::before::Before<E, F>
              poem::endpoint::catch_all_error::CatchAllError<E, F, R>
            and 22 others
    = note: required for `impl poem::Endpoint` to implement `poem::endpoint::endpoint::IntoEndpoint`
note: required by a bound in `poem::endpoint::endpoint::EndpointExt::map_to_response`
   --> /home/wright/.cargo/git/checkouts/poem-5d35782906f249bc/0718a7e/poem/src/endpoint/endpoint.rs:210:24
    |
210 | pub trait EndpointExt: IntoEndpoint {
    |                        ^^^^^^^^^^^^ required by this bound in `poem::endpoint::endpoint::EndpointExt::map_to_response`
    = note: this error originates in the attribute macro `OpenApi` (in Nightly builds, run with -Z macro-backtrace for more info)

For more information about this error, try `rustc --explain E0277`.
error: could not compile `zero2prod` due to 4 previous errors

I would fix that and try again soon.

dreamerlzl avatar Oct 19 '22 12:10 dreamerlzl

Released in v1.3.47

sunli829 avatar Oct 19 '22 13:10 sunli829