FastAPI (Python) Why I get "Unsupported upgrade request." with POST request?

Viewed 4252

I have similar apps on Flask and FastAPI. When I do this curl requests with Flask, that is all right:

Without TLS:

curl -X POST -H "Content-Type: application/json" -d '{"method": "account.auth"}' http://X.X.X.X:5050/

{"error":0,"result":{"token":"XXX"}}

With TLS:

curl -X POST -H "Content-Type: application/json" -d '{"method": "account.auth"}' https://example.com:8443/api/

{"error":0,"result":{"token":"XXX"}}

!!! But with FastAPI I get another result:

Without TLS:

curl -X POST -H "Content-Type: application/json" -d '{"method": "account.auth"}' http://X.X.X.X:5050/

{"error":0,"result":{"token":"XXX"}}

With TLS:

curl -X POST -H "Content-Type: application/json" -d '{"method": "account.auth"}' https://example.com:8443/api/

Unsupported upgrade request.

How to fix problem with "Unsupported upgrade request."? And what is it? Flask are working with it normally.

2 Answers

This same issue usually seems to arise from incomplete uvicorn installations, but is usually related to websockets.

A solution for this issue might be to reinstall uvicorn with the recommended (by FastAPI) extras:

python3 -m pip uninstall uvicorn
python3 -m pip install uvicorn[standard]

I had this problem using java to access the api. The solution was to set HTTP Request to 1.1

var httpRequest = HttpRequest.newBuilder()
             .uri(URI.create("http://127.0.0.1:8000/jobs"))
             .version(HttpClient.Version.HTTP_1_1)
             .GET()
             .build();
Related