Fastapi: Allow JSON payload for GET calls

Created on 2 Sep 2020  路  6Comments  路  Source: tiangolo/fastapi

First check

  • [x ] I added a very descriptive title to this issue.
  • [x ] I used the GitHub search to find a similar issue and didn't find it.
  • [ x] I searched the FastAPI documentation, with the integrated search.
  • [ x] I already searched in Google "How to X in FastAPI" and didn't find any information.
  • [x ] I already read and followed all the tutorial in the docs and didn't find an answer.
  • [ x] I already checked if it is not related to FastAPI but to Pydantic.
  • [ x] I already checked if it is not related to FastAPI but to Swagger UI.
  • [ x] I already checked if it is not related to FastAPI but to ReDoc.
  • [x ] After submitting this, I commit to:

    • Read open issues with questions until I find 2 issues where I can help someone and add a comment to help there.

    • Or, I already hit the "watch" button in this repository to receive notifications and I commit to help at least 2 people that ask questions in the future.

    • Implement a Pull Request for a confirmed bug.

Example

Here's a self-contained minimal, reproducible, example with my use case:

```Python
from fastapi import FastAPI

app = FastAPI()

class Sample(BaseModel):
countryId: int

@router.get("/foo/bar",
response_model=Sample)
def get_sample(sample: Sample):
return {"Hello": "World"}

Description

  • Open the browser and call the GET endpoint /foo/bar.
  • Pass in JSON body with {"countryId": 1}.
  • TypeError: Failed to execute 'fetch' on 'Window': Request with GET/HEAD method cannot have body.

The solution you would like

It doesn't throw the TypeError and allows GET calls to pass in JSON payloads

Environment

  • OS: [e.g. Linux / Windows / macOS]: macOS
  • FastAPI Version [e.g. 0.3.0]: 0.60.1

  • Python version:

3.7.7

Additional context

Screen Shot 2020-09-02 at 2 00 10 PM

enhancement

Most helpful comment

@ArcLightSlavik thanks, just read that publication ,actually you were right, in practice it's fine but in theory it's not a good idea i think.. :face_with_head_bandage:

All 6 comments

It's not something we'll be able to solve. We have already done everything that we could.

You can follow the discussions and solution already merged:

To summarize:
image

The HTTP GET method requests a representation of the specified resource. Requests using GET should only retrieve data.

Request has body NO
Successful response has body YES

The HTTP specification says in section 4.3

A message-body MUST NOT be included in a request if the specification of the request method (section 5.1.1) does not allow sending an entity-body in requests.

@ycd The HTTP specification your using is from 1991. The updated one is RFC 7231 where it is allowed to include a body.

   A payload within a GET request message has no defined semantics;
   sending a payload body on a GET request might cause some existing
   implementations to reject the request.

So generally it is allowed but discouraged.
OpenAPI take on it: https://github.com/OAI/OpenAPI-Specification/pull/1937

@ArcLightSlavik thanks, just read that publication ,actually you were right, in practice it's fine but in theory it's not a good idea i think.. :face_with_head_bandage:

An update to this issue, i found this when looking for something else,

GET, DELETE and HEAD are no longer allowed to have request body because it does not have defined semantics as per RFC 7231.

since FastAPI is based on OpenAPI specification i do not think we are able to this.

GET, DELETE and HEAD are no longer allowed to have request body because it does not have defined semantics as per RFC 7231.

That's true for OpenAPI 3.0, but seems to be changed in 3.1 as this PR suggests. The same change was removed from 3.0 only because it did't fit the semantics of a patch release

According to the PR, 3.1 will read as follows, which is in-line with RFC 7231.

In other cases where the HTTP spec is vague (such as GET, HEAD and DELETE), requestBody is permitted but does not have well-defined semantics and SHOULD be avoided if possible.

Was this page helpful?
0 / 5 - 0 ratings