Use Pydantic BaseModel json method for test request
See original GitHub issueFirst check
- I used the GitHub search to find a similar issue and didn’t find it.
- I searched the FastAPI documentation, with the integrated search.
- I already searched in Google “How to X in FastAPI” and didn’t find any information.
Description
How can I use pydanic BaseModel json method to generate json data for my test request?
Additional context
For example I have a endpoint that accepts a Action(BaseModel)
and for testing it I want to create a object of Action and use .json then pass it to client.post(json=action.json).
request = DoActionRequest(
game_id=self.game.id,
action=Action(
action_type=Action.ActionType.CAPTAIN_CALL_FOR_AN_ATTACK
),
payload=None
)
headers = auth_header_generator(self.game.get_fd_caption())
response = self.client.post(
self.url, json=request.json(), headers=headers
)
This gives me 422 http status code. but when I use the value of request.json() like
{"game_id": "1", "action": {"action_type": "call for an attack", "action_data": null}, "payload": null}
It works fine.
Issue Analytics
- State:
- Created 4 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Validate JSON Documents in Python using Pydantic
This article shows you how to validate your JSON documents against a specified schema using the popular Python library pydantic.
Read more >How to mock pydantic BaseModel that expects a Response ...
(I'm going to assume the Response is from the requests library. If it isn't, then appropriately adjust the attributes and methods to be...
Read more >Request Body - FastAPI
To declare a request body, you use Pydantic models with all their power and benefits. Info. To send data, you should use one...
Read more >How we validate input data using pydantic
Pydantic is a Python package for data parsing and validation, based on type hints. We use pydantic because it is fast, does a...
Read more >Parsing and validating data in Python using Pydantic
This is useful especially when we have complex nested data. We no longer need to parse JSON's to dictionaries. We can use Pydantic...
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 @koxudaxi for your help here! 🙇♂️
Thanks @Glyphack for reporting back and closing the issue.
Hey @koxudaxi It’s working in this way thank you. I’ll close this issue.