首页 > 解决方案 > Chrome.webRequest.onBeforeRequest - 在 POST 上传文件,但 requestBody.raw 为空

问题描述

在上传文件和表单数据时,我正在尝试使用 chrome 扩展来获取 POST 请求的正文。

使用以下代码块:

/// background.js ///
chrome.webRequest.onBeforeRequest.addListener(
    // callback
    function (details) {
        if (details.requestBody.raw) {
            for (let i = 0; i < details.requestBody.raw.length; i++) {
                var postedString = decodeURIComponent(String.fromCharCode.apply(null, new Uint8Array(details.requestBody.raw[i].bytes)));
                console.log(postedString)
            }
        }
        return { cancel: false }
    },
    {
        urls: ["https://*/*", "http://*/*"]
    },
    ["blocking", "requestBody"]
);

但是,我无法从控制台看到上传文件的内容文本(但我只能看到表单数据,message=Hello)。

------WebKitFormBoundaryINmsBoKGn0M78aYv
Content-Disposition: form-data; name="message"

Hello
------WebKitFormBoundaryINmsBoKGn0M78aYv
Content-Disposition: form-data; name="file"; filename="test.txt"
Content-Type: text/plain

manufest.json 是下面的代码块。

{
  "name": "testo_plugin",
  "description": "Build an test_plugin!",
  "version": "1.0.0",
  "manifest_version": 2,
  "background": {
    "page": "background.html"
  },
  "permissions": [
    "storage",
    "webRequest",
    "webRequestBlocking",
    "https://*/*",
    "http://*/*"
  ]
}

我的语法有问题吗?或者它是 Chrome 的规范?(我找不到这个规范)

Chrome版本:91.0.4472.114(正式版)(64位)

标签: google-chrome-extension

解决方案


推荐阅读