sindresorhus / got

🌐 Human-friendly and powerful HTTP request library for Node.js
MIT License
14.27k stars 935 forks source link

HTTP2 example returns `1.1 nghttpx` instead of `2 nghttpx` #1908

Closed paya-cz closed 2 years ago

paya-cz commented 3 years ago

Describe the bug

Unable to confirm Got works with HTTP2 by following documented example.

Actual behavior

Console prints:

1.1 nghttpx

Expected behavior

Console prints:

2 nghttpx

Code to reproduce

Code from: https://github.com/sindresorhus/got/tree/v11.8.2#http2

const got = require('got');

(async () => {
    const {headers} = await got('https://nghttp2.org/httpbin/anything', {http2: true});
    console.log(headers.via);
})();

Fix

Use a different server:

const got = require('got');

(async () => {
    const body = await got('https://http2.pro/api/v1', {http2: true}).json<any>();
    console.log('http2', body.http2);
})();
sindresorhus commented 3 years ago

From the docs:

This option requires Node.js 15.10.0 or newer as HTTP/2 support on older Node.js versions is very buggy.

I would also recommend trying Got v12 beta, which will be out in a final version soon: https://github.com/sindresorhus/got/releases/tag/v12.0.0-beta.4

paya-cz commented 3 years ago

@sindresorhus I have tried accessing https://nghttp2.org/httpbin/anything via Chromium and it returns Via header as 1.1, yet the Chromium network tab clearly shows the protocol as h2. I suspect the nghtt2.org server has a buggy HTTP2 detector (perhaps they started using reverse proxy?). The http2.pro server I suggested seems to detect H2 properly. So I think this has nothing to do with Got, it's just that the example in Got documentation no longer shows what it's meant to show.

szmarczak commented 3 years ago

It uses HTTP/2 but the proxy server is called 1.1 nghttpx, probably because the request is being converted to HTTP/1.1 server-side. The example is just outdated, we can check socket.alpnProtocol instead.