Home > Mobile >  FastAPI does not replace " " plus symbol in GET request
FastAPI does not replace " " plus symbol in GET request

Time:10-20

I understand this is not a FastAPI issue, but how to avoid this using FastAPI?

For example:

from fastapi import FastAPI

app = FastAPI()


@app.get('/')
async def root(q: str):
    return {"message": f"{q}"}

With request:

http://127.0.0.1:8000/?q=1 1

Return response:

{"message":"1 1"}

CodePudding user response:

is a reserved character in URLs which is used to denotate whitespace (as your result indicates). URL-encode your values to avoid this:

http://127.0.0.1:8000/?q=1+1

CodePudding user response:

The sign has a semantic meaning in the query string, i.e., representing the space. Similarly, the & sign, which is used to separate the various key=value pairs in the query string.

When a request arrives, FastAPI processes the query parameters after decoding the URL, and hence, any signs in the query string are decoded to a space. If you would like the sign to be preserved, you should encode the query parameters in the URL before issuing the HTTP request, so that all signs are converted to +. Then, when your FastAPI server decodes the query string, all + will be converted back to signs.

In JavaScript, you can use the encodeURI() function, which takes as argument a complete URI:

var encodedURL = encodeURI('http://127.0.0.1:8000/?q=1 1');

or, use the encodeURIComponent function, which takes any object (such as string and number):

var encodedURL =  'http://127.0.0.1:8000/?q='   encodeURIComponent('1 1');

If you are sending the request directly from the browser (i.e., by typing the URL in the address bar of the browser), then make sure to send it URL-encoded:

http://127.0.0.1:8000/?q=1+1

If you still want to send the request in this format http://127.0.0.1:8000/2?q=1 1, and get back the response preserving the signs, you can use request.url.query, which will return the raw query string, allowing you to split the key=value pairs and get the value of q parameter in the original form. Example:

from fastapi import Request

@app.get('/')
def root(request: Request):
    q = request.url.query.split('&')[0].split('=')[1]
    return {'message': q}
  • Related