Alex Poloz
Alex Poloz

Reputation: 486

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

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.

Upvotes: 6

Views: 9744

Answers (5)

westman379
westman379

Reputation: 723

If you connect from Java/Kotlin client to Python server, then it can be solved easily by setting RequestFactory on client side like this:

requestFactory = OkHttp3ClientHttpRequestFactory()

Upvotes: 0

Lsk
Lsk

Reputation: 11

my solution is based on Maikel's approach. the development environment is Spring Boot 3.x, and I am using the JDK client HTTP connector. Its default protocol version is HTTP/2.0, and we need to manually obtain an HTTP/1.1 connector. This can be done as follows.

        var httpServiceProxyFactory = HttpServiceProxyFactory
            .builder(
                WebClientAdapter.forClient(
                    WebClient.builder()
                        .baseUrl("xxx")
                        .clientConnector(new JdkClientHttpConnector(
                            HttpClient.newBuilder()
                                .version(HttpClient.Version.HTTP_1_1)
                                .build()
                        ))
                        .defaultHeader(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE)
                        .defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
                        .build()
                )
            )
            .blockTimeout(Duration.ofDays(1))
            .build();

Upvotes: 1

The Prototype
The Prototype

Reputation: 274

In my case, the Java Spring Boot application tried to make a request via the RestClient to the Python server, and I received the error "422 Unprocessable Entity" in response and in the python server logs there was an error WARNING: Unsupported upgrade request.

As far as I understood, this was due to the fact that the python server does not support RestClient requests with HTTP 2.0 version, as a temporary (or maybe permanent) solution the server uvicorn has been replaced by hypercorn (usage) server and everything started working as expected.

Upvotes: 1

Maikel
Maikel

Reputation: 151

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();

Upvotes: 12

Jason Rebelo Neves
Jason Rebelo Neves

Reputation: 1281

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]

Upvotes: 11

Related Questions