Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
dreamerlzl opened this issue Sep 21, 2022 · 7 comments
Labels
question Further information is requested

Comments

@dreamerlzl
Copy link

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 dreamerlzl added the question Further information is requested label Sep 21, 2022
@dreamerlzl dreamerlzl changed the title charset=utf-8 causes application/x-www-form-urlencoded parse error charset=utf-8 for application/x-www-form-urlencoded causes HTTP response 415 Sep 21, 2022
@sunli829
Copy link
Collaborator

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

@banool
Copy link
Contributor

banool commented Sep 26, 2022

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
Copy link
Contributor

banool commented Sep 26, 2022

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.

@brunobell
Copy link
Contributor

@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.

sunli829 added a commit that referenced this issue Oct 18, 2022
@sunli829
Copy link
Collaborator

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

sunli829 added a commit that referenced this issue Oct 19, 2022
@dreamerlzl
Copy link
Author

dreamerlzl commented Oct 19, 2022

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.

@sunli829
Copy link
Collaborator

Released in v1.3.47

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

4 participants