Search code examples
pythonhttpfastapiurlencodestarlette

FastAPI does not replace "+" plus symbol in GET request


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}"}

Issuing the following request:

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

returns:

{"message":"1 1"}

Solution

  • The plus sign (+) has a semantic meaning in the query string, i.e., representing the space character. Similarly, the ampersand 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 %2B. Then, when your FastAPI server decodes the query string, all %2B 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%2B1
    

    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}