简体   繁体   English

如何使用 Puppeteer 和 Chrome DevTools 协议修改请求标头? (可能是JS语法问题)

[英]How to modify request headers using Puppeteer & Chrome DevTools Protocol? (Possibly a JS syntax issue)

I have the following Typescript function that assumes a Chrome browser has already been launched using Puppeteer.我有以下 Typescript 函数,假设已经使用 Puppeteer 启动了 Chrome 浏览器。 The documentation for the Fetch functions used below can be found here .下面使用的 Fetch 函数的文档可以在这里找到。

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,
        });
    });
}

Here is the specific error I'm seeing:这是我看到的具体错误:

Error: Protocol error (Fetch.continueRequest): Invalid parameters headers: array expected错误:协议错误(Fetch.continueRequest):无效的参数标头:需要数组

How can I resolve this error and successfully modify the request.headers ?如何解决此错误并成功修改request.headers Is this a silly Javascript/Typescript syntax issue that I just can't figure out?这是我无法弄清楚的愚蠢的 Javascript/Typescript 语法问题吗?

Fetch.requestPaused returns the headers as an object. Fetch.requestPaused将标头作为对象返回。 eg:例如:

{
    "Upgrade-Insecure-Requests":"1",
    "Accept": "text/html,application/xhtml+xml"}
}

Fetch.continueRequest expects an Array<{name: string, value: string}> . Fetch.continueRequest需要一个Array<{name: string, value: string}> eg例如

[
    {"name": "Accept", value: "text/html,application/xhtml+xml"}
]

You can use the code that Puppeteer is using:您可以使用 Puppeteer 正在使用的代码:

function headersArray(headers) {
  const result = [];
  for (const name in headers) {
    if (!Object.is(headers[name], undefined))
      result.push({name, value: headers[name] + ''});
  }
  return result;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在没有 Puppeteer 的情况下通过 DevTools 协议模拟 Chrome window 中的点击? - How to simulate clicks in a Chrome window via the DevTools Protocol without Puppeteer? 如何使用Chrome DevTools Protocol的printToPDF修改first pageNumber或执行header或footer模板中的JS - How to modify the first pageNumber or execute JS in header or footer template with Chrome DevTools Protocol's printToPDF Next.js中如何修改请求头 - How to modify request headers in Next.js 如何在 Chrome DevTools 中调试 ajax 请求中加载的 js - How to debug js loaded in ajax request in Chrome DevTools 如何重定向到chrome-devtools://协议? - How to redirect to the chrome-devtools:// protocol? 如何使用野生动物园扩展名修改请求标头 - How modify the request headers using safari extension 如何使用 Node JS Puppeteer 在无头 chrome 请求中设置代理服务器 - how do I set a proxy server in my headless chrome request using Node JS Puppeteer 如何根据 Chrome 扩展中的用户选项修改 http 请求标头? - How to modify http request headers based on user options in a Chrome extension? 如何在单独的 window(分离)上自动打开带有 chrome window 和 devtools 的 puppeteer? - How to open puppeteer with chrome window and devtools on a separate window (detached) automatically? 在Chrome Devtools协议中,什么是`injectScriptId`? - In Chrome Devtools Protocol, what is `injectedScriptId`?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM