Skip to content

Will FastAPI support QUERY http method? "app.query" #12965

@Kludex

Description

@Kludex

Discussed in #6049

Originally posted by FilipeMarch December 16, 2022

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.

Commit to Help

  • I commit to help with one of those options 👆

Example Code

from fastapi import FastAPI

app = FastAPI()


@app.query('/query/subjects')
def query_subjects(schema: ArbitrarySchema):
    with Session(engine) as db:
        subjects = db.query(Subject).all()
        return schema(**subjects)

# something like this

Description

There is a new HTTP method called QUERY, I discovered it this week and it is super interesting!

https://www.ietf.org/archive/id/draft-ietf-httpbis-safe-method-w-body-02.html

I was trying to make a GET route that would accept a request body, but started receiving an error on Swagger UI:

TypeError: Request has method 'GET' and cannot have a body

The idea is that sometimes we need to make a big or complex query, and this is the scenario in which we can see the advantages of GraphQL, and although I have seen that we can integrate FastAPI with GraphQL, I was wondering if FastAPI will ever be able to simply accept a QUERY method like, for example, app.query("/query/subjects").

For example, suppose I have this object:

{
   "id": 1,
   "name": "Math"
   "tags": [
       {
           "id": 1,
           "name": "Algebra",
           "number_of_clicks": 1,
           "number_of_questions": 7,
           "number_of_answers": 3,
           "number_of_comments": 2,
           "number_of_votes": 1,
       }]
   "topics": [
       {
           "id": 1,
           "name": "Linear Equations",
           "likes": 1,
           "dislikes": 0,
           "number_of_clicks": 1,
           "number_of_tutorials": 1,
           "number_of_questions": 7,
           "posts": [
               {
                   "id": 1,
                   "title": "How to solve linear equations?",
                   "likes": 1,
                   "dislikes": 0,
                   "number_of_clicks": 1,
                   "number_of_answers": 3,
                   "number_of_comments": 2,
                   "number_of_votes": 1,
                   "answers": [
                       {
                           "id": 1,
                           "content": "You can solve linear equations by using the substitution method.",
                           "likes": 1,
                           "dislikes": 0,
                           "number_of_clicks": 1,
                           "number_of_comments": 2,
                           "number_of_votes": 1,
                           "comments": [
                               {
                                   "id": 1,
                                   "content": "That's a great answer!",
                                   "likes": 1,
                                   "dislikes": 0,
                                   "number_of_clicks": 1,
                                   "number_of_votes": 1,
                               }"
                          ]
                       }
                   ]
               }
           ]
       }
   ]
}

I want the client to be able to QUERY this in any specific way, maybe he wants GET objects with this format

{
   "id": 1,
   "name": "Math"
   "tags": [  
       {
           "id": 1,
           "name": "Algebra",
       }]
   "topics": [
       {
           "id": 1,
           "name": "Linear Equations",
       }
   ]
}

But it could be totally different, and I can't predict the way the client is gonna query it and make a new route for every combination and possible schema for a complex object. The QUERY route would accept a request body, and then we would filter the response with the schema the client sends on request body.

  • Would it be too complicated to implement this on FastAPI?
  • Is there any hope that this will someday be implemented in FastAPI? XD

Thanks!

Wanted Solution

...

Wanted Code

...

Alternatives

...

Operating System

macOS

Operating System Details

No response

FastAPI Version

A

Python Version

3.11

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    featureNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions