[QUESTION] One or more objects in request body?
See original GitHub issueDescription
I’d like to have an endpoint in my API that accepts one or more objects in the request body. For example, I would like both of these to be valid request bodies:
{
"name": "foo"
}
[
{"name": "foo"},
{"name": "bar"}
]
I’m not sure how to do this in fastapi/pydantic. Is it even possible? Any advice?
Issue Analytics
- State:
- Created 4 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Is it possible to pass multiple objects in @RequestBody?
Here i am assuming that you want to send two class objects in a single responsebody response. Create an additional inner class in...
Read more >Error : JSON request body must be an object
@kishore Try the suggested solution from @devendra as: req.setBody('{"jsonBodyList":' + JSON.serialize(jsonBodyList) + '}' and with the above ...
Read more >JSON request body with Arrays - Appian Community
Integration Object - Test Request - JSON request body with Arrays ... the request body based on the solution to the above question...
Read more >Request and response objects - Django documentation
The raw HTTP request body as a bytestring. This is useful for processing data in different ways than conventional HTML forms: binary images,...
Read more >Programatically change the request body in Postman - YouTube
Mutating the request body in Postman is something that many people ... me create more videos like this one, please consider subscribing.
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
@kevlar1818
Union
is currently somewhat finicky in pydantic due to the way it is implemented – the order matters because it will try to parse types in the provided order. For what it’s worth, if you have issues with pydanticUnion
parsing, I would encourage you to open a github issue over on the pydantic repository – if the problem isn’t a bug (which it might be), you’ll probably get a suggestion for how to accomplish your goal.For what it’s worth, the following script does work for me:
If your issue with the container approach is that you don’t want to write a container model for every list-or-object type you want to have, you can leverage
pydantic.generics.GenericModel
:(The above script runs for me as is.)
I believe this plays nicely with FastAPI as well.
Thanks for the help @dmontagu 🌮!
Thanks @kevlar1818 for reporting back and closing the issue.