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.

[QUESTION] How can I validate request body as is?

See original GitHub issue

Description

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:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
tiangolocommented, Oct 26, 2019

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 of str before trying to convert it to something else: https://pydantic-docs.helpmanual.io/#custom-data-types

Read more comments on GitHub >

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

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