告诉我们您的环境:
预期的结果是什么?
我想在 header 中覆盖请求的主机。
如请求url:
http://a.com/x.jpg
. DNS 解析器可能会将主机解析为较慢的 ip 地址,但我知道有一个更好的 ip,例如 1.2.3.4
.我想用 ip 地址更改 url 并修改请求 header 中的主机。喜欢来自
curl http://a.com/x.jpg
至curl --header "Host:a.com" http://1.2.3.4/x.jpg
相反会发生什么? await page.setRequestInterception(true);
page.on('request', request => {
const newHeaders = Object.assign({}, request.headers(), {
Host: 'a.com'
});
request.continue({ headers: newHeaders, url: 'http://1.2.3.4/x.jpg' });
但它不起作用
最佳答案
您应该使用 request.respond 来覆盖标题,如下所示:
page.on('request', request => {
request.respond({
headers:{...request._headers,your_new_header_key:value},
body
})
});
关于javascript - puppeteer request.continue 无法使用覆盖的 header 主机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60539833/