You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Chrome扩展开发:chrome.webRequest无法捕获PATCH请求的requestBody问题及解决方案咨询

Chrome扩展开发:chrome.webRequest无法捕获PATCH请求的requestBody问题及解决方案咨询

我正在开发一个Chrome扩展,用来捕获Web应用中的请求参数及其值。目前已经成功通过chrome.webRequest API捕获到POST请求的requestBody,但在测试更新操作(PATCH请求)时,requestBody返回的是null。

我给函数加了日志语句排查,最后推测这可能是chrome.webRequest API本身的限制。我对比了请求头信息,发现POST请求里有requestBody相关标识,但PATCH请求里没有,这进一步验证了我的猜测。

以下是我当前的代码实现:

// Capture the request body before the request is sent 
chrome.webRequest.onBeforeRequest.addListener(   
(details) => {     
if (isCapturing && ["POST", "PUT", "PATCH"].includes(details.method)) { 
      const requestBody = details.requestBody;       
       let rawBody = "";       
console.log(`Request details: ${JSON.stringify(details)}`);  

if (requestBody) {
    console.log(`Request body exists for ${details.method} request to ${details.url}`);
    if (requestBody.raw && requestBody.raw.length) {
      rawBody = String.fromCharCode.apply(
        null,
        new Uint8Array(requestBody.raw[0].bytes)
      );
      console.log(`Raw body extracted for ${details.method} request to ${details.url}`);
    } else {
      console.log(`Request body raw is empty for ${details.method} request to ${details.url}`);
    }
  } else {
    console.log(`Request body is null for ${details.method} request to ${details.url}`);
  }
  const url = new URL(details.url);
  const postmanUrl = {
    raw: details.url,
    protocol: url.protocol.replace(":", ""),
    host: url.hostname.split("."),
    path: url.pathname.split("/").filter(Boolean),
  };

  // Push the request with the body
  capturedRequests.push({
    name: details.url,
    request: {
      method: details.method,
      header: [], // Will be added in onCompleted
      body: rawBody,
    },
  });
}
}, 
{ urls: ["<all_urls>"] }, 
["requestBody"] );

我想知道有没有什么变通方法,能让这个功能同时支持POST和PATCH请求?如果有已知的解决方案,麻烦告知,非常感谢!

备注:内容来源于stack exchange,提问作者Sayuru Rehan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.14 13:20:28