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.

Use Pydantic BaseModel json method for test request

See original GitHub issue

First 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:closed
  • Created 4 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
tiangolocommented, Apr 6, 2020

Thanks @koxudaxi for your help here! 🙇‍♂️

Thanks @Glyphack for reporting back and closing the issue.

1reaction
Glyphackcommented, Apr 2, 2020

Hey @koxudaxi It’s working in this way thank you. I’ll close this issue.

Read more comments on GitHub >

github_iconTop 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 >

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