Skip to content
Advertisement

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:

JavaScript

Issuing the following request:

JavaScript

returns:

JavaScript

Advertisement

Answer

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:

JavaScript

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

JavaScript

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:

JavaScript

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:

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement