Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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

Support request bodies for GET requests? #1691

Closed
9 tasks done
gjdanis opened this issue Jul 8, 2020 · 4 comments
Closed
9 tasks done

Support request bodies for GET requests? #1691

gjdanis opened this issue Jul 8, 2020 · 4 comments

Comments

@gjdanis
Copy link

gjdanis commented Jul 8, 2020

First check

  • I added a very descriptive title to this issue.
  • I used the GitHub search to find a similar issue and didn't find it.
  • I searched the FastAPI documentation, with the integrated search.
  • I already searched in Google "How to X in FastAPI" and didn't find any information.
  • I already read and followed all the tutorial in the docs and didn't find an answer.
  • I already checked if it is not related to FastAPI but to Pydantic.
  • I already checked if it is not related to FastAPI but to Swagger UI.
  • I already checked if it is not related to FastAPI but to ReDoc.
  • After submitting this, I commit to one of:
    • Read open issues with questions until I find 2 issues where I can help someone and add a comment to help there.
    • 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:

import uvicorn
from pydantic import BaseModel
from fastapi import FastAPI


class Item(BaseModel):
    name: str
    description: str


app = FastAPI()


@app.get("/items")
def create_item(item: Item):
    return item


if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", port=5000, debug=True)

You can run and test the above code with this CURL request:

curl -X GET \
  http://localhost:5000/items \
  -d '{
	"name": "test",
	"description": "tester"
}
'

Description

FastAPI's documentation seems to suggest that GET requests with a body aren't supported: https://fastapi.tiangolo.com/tutorial/body/

However, running the above example it looks like this is supported, although the auto generated documentation misses documenting the parameters of the body.

Can this be supported in FastAPI? Our use case is that we need to support a GET method that could contain a sizable amount of data in the request. We don't really want to make this a POST method as the endpoint is only for retrieval and isn't modifying any data. Everything appears to be working for us except the auto generated documentation.

Environment

  • OS: [e.g. Linux / Windows / macOS]: Windows
  • FastAPI Version [e.g. 0.3.0]: 0.54.1
  • Python Version: 3.8.2
@gjdanis gjdanis added the question Question or problem label Jul 8, 2020
@Kludex
Copy link
Sponsor Collaborator

Kludex commented Jul 8, 2020

I don't recall it properly, but I saw an issue talking about it... It seems like Swagger didn't support previously, but they are going to support/or are already supporting. There's an open PR related to what you're talking about, you might want to check: #1626

@tiangolo
Copy link
Owner

tiangolo commented Dec 6, 2020

Yeah, that's currently supported by FastAPI in recent versions (the current is 0.62.0), but discouraged, as it's not part of the web standards. And the docs with Swagger UI will probably not show the optional GET body, but curl should work.

@terminalmage
Copy link

terminalmage commented Apr 23, 2021

I have kind of the opposite issue, in that I have not defined a request body for my GET requests, but swagger is prepopulating it with:

[
    "string"
]

This forces me to always remove the request body before executing, when running any API calls through the swagger UI.

I've searched for how to hide this, but the only examples I've found so far are for Java applications.

EDIT: Please disregard. This was caused by a bad type hint. Instead of some_arg: Union[None, str] = None, I had used some_arg: Union[None, list] = None.

@github-actions
Copy link
Contributor

Assuming the original need was handled, this will be automatically closed now. But feel free to add more comments or create new issues or PRs.

@tiangolo tiangolo reopened this Feb 28, 2023
Repository owner locked and limited conversation to collaborators Feb 28, 2023
@tiangolo tiangolo converted this issue into discussion #7333 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

4 participants