Skip to content

Commit

Permalink
fix: 处理header头使用application/x-www-form-urlencoded时,需传入Content-Length,…
Browse files Browse the repository at this point in the history
…不然后端无法获取body中值
  • Loading branch information
fffguo committed Aug 19, 2022
1 parent 08adc42 commit aea016f
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions utools/preload.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ sendRequest = function (args, callback, errorCallback) {
port: myURL.port === "" ? (args.url.startsWith('https') ? 443 : 80) : myURL.port,
path: myURL.pathname + myURL.search,
method: args.method,
headers: filterHeader(args.headers),
headers: handleHeader(args.headers, args.body),
};
let request;
if (args.url.startsWith('https')) {
Expand All @@ -42,11 +42,14 @@ sendRequest = function (args, callback, errorCallback) {
}
}

let filterHeader = function (headers) {
let handleHeader = function (headers, body) {
let newHeaders = {}
for (const key in headers) {
if ("Content-Type" === key && headers[key] === "application/x-www-form-urlencoded" && body !== undefined) {
newHeaders['Content-Length'] = Buffer.byteLength(body)
}
if ("Accept-Encoding" !== key) {
newHeaders[key] = headers[key]
newHeaders[key] = headers[key];
}
}
return newHeaders
Expand Down

0 comments on commit aea016f

Please sign in to comment.