【发布时间】:2019-08-21 17:57:39
【问题描述】:
我有以下 Typescript 函数,假设 Chrome 浏览器已经使用 Puppeteer 启动。下面使用的 Fetch 函数的文档可以在 here 找到。
async function modify(client: CDPSession) {
client.on('Fetch.requestPaused', async ({ requestId, request, frameId, resourceType, responseErrorReason, responseStatusCode, responseHeaders, networkId }) => {
// Correctly prints out the User-Agent header's value
console.log(request.headers["User-Agent"]);
// After this line is run, I can inspect the request.headers object and see that User-Agent was successfully edited
request.headers['User-Agent'] = 'trying to edit this header';
// Continuing the request gives an error
await client.send('Fetch.continueRequest', {
requestId: requestId,
headers: request.headers,
});
});
}
这是我看到的具体错误:
错误:协议错误(Fetch.continueRequest):无效参数标头:预期数组
如何解决此错误并成功修改request.headers?这是我无法弄清楚的愚蠢的 Javascript/Typescript 语法问题吗?
【问题讨论】:
-
send函数中的headers是数组,所以需要设置[{name: 'User-Agent', value: 'trying to edit this header'}]
标签: javascript node.js typescript puppeteer chrome-devtools-protocol