[QUESTION] A way to handle multiple request content types
See original GitHub issueOpenAPI 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:
- Created 4 years ago
- Reactions:15
- Comments:15 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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.
@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.