Thanks to visit codestin.com
Credit goes to github.com

Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Allow JSON payload for GET calls #2004

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
stevesuh opened this issue Sep 2, 2020 · 9 comments
Closed

Allow JSON payload for GET calls #2004

stevesuh opened this issue Sep 2, 2020 · 9 comments

Comments

@stevesuh
Copy link

stevesuh commented Sep 2, 2020

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:

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](https://user-images.githubusercontent.com/5025518/92019556-ca7cbf00-ed24-11ea-84d6-52dbc18a8aec.png)
@stevesuh stevesuh added the feature New feature or request label Sep 2, 2020
@Kludex
Copy link
Member

Kludex commented Sep 2, 2020

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

@ycd
Copy link
Contributor

ycd commented Sep 2, 2020

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.

@ArcLightSlavik
Copy link
Contributor

@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: OAI/OpenAPI-Specification#1937

@ycd
Copy link
Contributor

ycd commented Sep 3, 2020

@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.. πŸ€•

@ycd
Copy link
Contributor

ycd commented Oct 24, 2020

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.

@miraculixx
Copy link

miraculixx commented Nov 26, 2020

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.

@ycd
Copy link
Contributor

ycd commented Dec 30, 2020

@stevesuh the code piece works when you send a request with cURL

curl -X GET "http://127.0.0.1:8000/foo/bar" -d "{\"countryID\":1}"

So basically FastAPI allows request body with GET operations, however, Swagger UI doesn't support it.

They are also very strict about not supporting it.

See: swagger-api/swagger-ui#2136

If you want you can open another issue in Swagger UI's repository but FastAPI can not do anything about this topic since your issue is completely about the Swagger UI.

@stevesuh
Copy link
Author

stevesuh commented Jan 3, 2021

I consider the issue resolved then.

@stevesuh stevesuh closed this as completed Jan 3, 2021
@tiangolo
Copy link
Member

tiangolo commented Nov 9, 2022

Thanks for the help here everyone! πŸ‘ πŸ™‡

Thanks for reporting back and closing the issue @stevesuh πŸ‘

Sorry for the long delay! πŸ™ˆ I wanted to personally address each issue/PR and they piled up through time, but now I'm checking each one in order.

@tiangolo tiangolo added question Question or problem answered reviewed and removed feature New feature or request labels Feb 22, 2023
@tiangolo tiangolo reopened this Feb 28, 2023
@fastapi fastapi locked and limited conversation to collaborators Feb 28, 2023
@tiangolo tiangolo converted this issue into discussion #7275 Feb 28, 2023

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Projects
None yet
Development

No branches or pull requests

6 participants