我想使用 Node.js 的http2库通过代理将 HTTP/2 请求发送到服务器。
出于测试目的,我使用 Charles v4.2.7 作为代理,但 Charles 无法代理请求。Charles 显示Malformed request URL "*"错误,因为它收到的请求是PRI * HTTP/2.0(HTTP/2 连接前言)。我可以使用 cURL(例如curl --http2 -x localhost:8888 https://cypher.codes)通过我的 Charles 代理成功发送 HTTP/2 请求,所以我认为这不是 Charles 的问题,而是我的 Node.js 实现的问题。
这是我的 Node.js HTTP/2 客户端实现,它尝试通过我的 Charles 代理监听 http://localhost:8888向https://cypher.codes发送 GET 请求:
const http2 = require('http2');
const client = http2.connect('http://localhost:8888');
client.on('error', (err) => console.error(err));
const req = client.request({
':scheme': 'https',
':method': 'GET',
':authority': 'cypher.codes',
':path': '/',
});
req.on('response', (headers, flags) …Run Code Online (Sandbox Code Playgroud)