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.

[BUG] Body is always parsed as JSON regardless of media_type

See original GitHub issue

Describe the bug

This is to continue the discussion at https://github.com/tiangolo/fastapi/issues/579#issuecomment-589263249. Body accepts an argument media_type, but regardless of the media_type Body is parsed as JSON. This is a problem for other media_types such as plain/text, application/sql, etc. And this is not compliant to OpenAPI content. Body should be able to represent plain text as str too.

Basically I wish I could do this:

request: str = Body(..., media_type='text/plain')

However, this produces 400 parsing error because Body is parsed as JSON anyway.

To Reproduce

  1. Create a file with:
from fastapi import FastAPI, Body

app = FastAPI()

@app.post("/")
def read_root(body: str = Body(..., media_type='text/plain')):
    return {"I received:": body}
  1. Run the server.

  2. Then execute: curl -d 'I want to print this' -H 'Content-Type: plain/text' http://localhost:8000

  3. It returns {“detail”:“There was an error parsing the body”}

  4. But I expected it to return {"I received": "I want to print this"}.

Expected behavior

My expectation is that the Body should be parsed based on the Content-Type header always.

Screenshots

I think the information is quite comprehensive without screenshots.

Environment

  • OS: Ubuntu 18.04
  • FastAPI Version: 0.49.0
  • Python 3.7.4

Additional context

The associated part is: https://github.com/tiangolo/fastapi/blob/9c3c9b6e78768374868d690bc05918d58481e880/fastapi/routing.py#L114

This does not check Content-Type. My proposal is to fix it as

if body_bytes and request.headers['Content-Type'] == 'application/json':
# if body_bytes and request.headers.get('Content-Type', 'application/json'): # if the content type should be assumed.
    body = await request.json()
else:
    body = body_bytes

I can make a PR for this, but I would like to learn the maintainers’ opinion on this. @tiangolo @dmontagu @phy25, thanks for the great project!

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:22
  • Comments:21 (2 by maintainers)

github_iconTop GitHub Comments

6reactions
coryvirokcommented, Jul 18, 2020

Any update on the fix for this? I’d love to be able to get my API docs to include the request body documentation for text/plain POST endpoints. 😃

4reactions
AdmiralNemocommented, Jun 10, 2020

Is there any other way to document the request body in OpenAPI documentation, besides Body(...)?

I have a legacy service that I am porting to FastAPI, and one of the operations accepts a YAML body. I am happy to parse it myself using the raw request, but I do not see any way to express that a) this operation accepts a body, or b) that the body should be application/yaml.

Most of the users of my service use the Swagger UI directly, so not being able to enter the body for the request there would be a huge problem for them.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Scala Body Parsers - 2.8.x - Play Framework
If you want to be a little more relaxed, you can instead use tolerantJson , which will ignore the Content-Type and try to...
Read more >
Post parameter is always null - Stack Overflow
Usually this problem is because your JSON object is incorrect. I have found this is always because in the constructor has an invalid...
Read more >
SyntaxError: JSON.parse: bad parsing - JavaScript | MDN
JSON.parse() parses a string as JSON. This string has to be valid JSON and will throw this error if incorrect syntax was encountered....
Read more >
Understanding JSON Schema
4 Since JSON strings always support unicode, they are analogous to unicode on Python 2.x and str on Python 3.x.
Read more >
Unirest in Java: Simplified, lightweight HTTP client library.
Errors in Object or JSON parsing. You can't always get what you want. And sometimes results you get from web services will not...
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