[QUESTION] How can I validate request body as is?
See original GitHub issueDescription
How can I validate request body as is?
Additional context
I try to use fastapi. First I wrote simple api, as shown below.
from fastapi import FastAPI, Body
app = FastAPI()
@app.post("/test")
def test(i: int = Body(..., embed=True),
f: float = Body(..., embed=True),
s: str = Body(..., embed=True),
b: bool = Body(..., embed=True)):
return {'int': i, 'float': f, 'str': s, 'bool': b}
And request with valid body shown below. I think this result is correct.
$ curl localhost:8000/test -X POST -d '{"i": 1, "f": 1.0, "s": "abc", "b": true}'
{"int":1,"float":1.0,"str":"abc","bool":true}
Then, I try to request with invalid body I think. Result is shown below.
$ curl localhost:8000/test -X POST -d '{"i": true, "f": true, "s": true, "b": true}'
{"int":1,"float":1.0,"str":"True","bool":true}
true
value is converted to each types(int, float, str).
When I want to validate request body as is, how can I do that?
Issue Analytics
- State:
- Created 4 years ago
- Reactions:1
- Comments:8 (4 by maintainers)
Top Results From Across the Web
java - Springboot - validate @RequestBody - Stack Overflow
Question : It is possible to validate the JSON payload of a request body, without specifically writing if statements?
Read more >How to validate Json Post Request body aginest a schema ...
var body = context.Request.Body.As<JObject>(preserveContent: true); // Use JArray if request body is array type; if (body["userId"] == null) // ...
Read more >Spring Boot - How to Validate HTTP POST Request Body?
Click the below link to download the Java Source code and PPT: ... soap and rest api, rest interview question, restful web services...
Read more >Validate Request Body and Parameter in Spring Boot - Medium
A typical Web application workflow is: to receive a request with some inputs, perform a treatment with the input received, and finally return...
Read more >Enable request validation in API Gateway - AWS Documentation
Learn how to enable request validation on methods for API Gateway. You can validate a request body by specifying a model schema or...
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
The request is validated by Pydantic. If you want to make sure that each type is exactly that type before doing any conversion, you can create a sub-class for each type, and then use Pydantic validators to ensure that it’s exactly that type.
Check the example for
StrictStr
. It ensures that the value is an instance ofstr
before trying to convert it to something else: https://pydantic-docs.helpmanual.io/#custom-data-typesSure, see here https://pydantic-docs.helpmanual.io/usage/types/#strict-types @carlosvbhenriques