question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[QUESTION] A way to handle multiple request content types

See original GitHub issue

OpenAPI allows describing multiple accepted content-types for a request. Is it possible via FastAPI means?

I’d like to make a password login endpoint accepting both application/json and application/x-www-form-urlencoded using a single handler.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:15
  • Comments:15 (3 by maintainers)

github_iconTop GitHub Comments

23reactions
tiangolocommented, Jun 5, 2020

Thanks for the help here everyone! 🚀

Yeah, the way to support different content types on the same path operation would be to read the request directly and parse it depending on the content type.

But then you have to do the data validation and documentation in OpenAPI yourself, in your code.

There wouldn’t be an easy way to do it more automatically based on type annotations as the body has to be consumed from the ASGI messages, and it would be a different process for each media type.

What I would try to do is to have a different path for each media type, that way I can enforce validation for the JSON or form data while reading the content directly for other complex types.

10reactions
Sharathmk99commented, Apr 18, 2020

@evstratbg there is no limitations like that in http protocol. I have implemented in other frameworks like flask, spring, etc… OpenAPI spec also supports multiple content type for one endpoint.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Sending multiple content type in single request - Stack Overflow
The api seems to support multiple content types. How can i form the header with two content types 1. Multipart form data (for...
Read more >
Content-Type - HTTP - MDN Web Docs - Mozilla
In requests, (such as POST or PUT ), the client tells the server what type of data is actually sent. Header type, Representation...
Read more >
HTTP headers | Content-Type - GeeksforGeeks
The Content-Type header is used to indicate the media type of the resource. The media type is a string sent along with the...
Read more >
HTTP/1.1: Header Field Definitions
Note: Use of the "q" parameter name to separate media type parameters from Accept ... If multiple content-codings are acceptable, then the acceptable ......
Read more >
Multiple Content Types or multiple document libraries?
Whether or not you use content types/metadata or folders, you always have to be aware of the total number of documents in a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found