I’m developing a chrome extension to capture the parameters and it’s values from a web application. So far I have been successfully able to capture the requestBody of POST calls through the chrome.webRequest API. But when testing the same for an update action (PATCH) the “requestBody” returns as null
I’ve tried adding log statements to my function and come to a conclusion that it might be a limitation in the chrome.webRequest API itself.
// 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"] );
I need to know if there is any workaround for this to make it work for both POST and PATCH.
I’ve logged the header details and found out that the tag “requestBody” is available in POST and not in PATCH. This could mean that there is a limitation. I need to know if there is any known workaround for this.
Any help would be appreciated!