2

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.

  • You can get the request URL and parse the query string by yourself – WENJUN CHI Feb 23 '22 at 12:25
  • Does this answer your question? [Getting Query Parameters as Dictionary in FastAPI](https://stackoverflow.com/questions/67393319/getting-query-parameters-as-dictionary-in-fastapi) – Gino Mempin Feb 23 '22 at 12:39

1 Answers1

5

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.

MatsLindh
  • 49,529
  • 4
  • 53
  • 84