user17607028
user17607028

Reputation:

How to get query params in fast api from POST request

Client might send multiple query params like:

# method = POST 
http://api.com?x=foo&y=bar

I need to get all the query params and extract it as a string x=foo&y=bar from the POST request.

Is there a way to do this on fast api? I could not find it on their docs.

NOTE :

We are not sure of the names of the params before hand.

Upvotes: 2

Views: 4726

Answers (1)

MatsLindh
MatsLindh

Reputation: 52792

Depending how you want to use the data, you can either use request.query_params to retrieve a immutable dict with the parameters, or you can use request.url to get an object representing the actual URL. This object has a query property that you can use to get the raw string:

from fastapi import FastAPI, Request
import uvicorn

app = FastAPI()

@app.get('/')
def main(request: Request):
    return request.url.query

 
uvicorn.run(app)

This returns the given query string /?foo=123 as foo=123 from the API. There is no difference between a POST or GET request for this functionality.

Upvotes: 5

Related Questions