From ea1134b937473b8708868f171732d8f51ff34d79 Mon Sep 17 00:00:00 2001 From: liufuguo <595726017@qq.com> Date: Fri, 29 Jul 2022 17:35:46 +0800 Subject: [PATCH 1/2] fix Content-Type bug --- index.js | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index c3f6cd2..46ca166 100644 --- a/index.js +++ b/index.js @@ -43,8 +43,12 @@ function curlString( return curl; } -function getHeader(options, headerKeyName) { - return options.headers[headerKeyName]; +function getLowHeaders(headers) { + let lowHeaders = {} + Object.keys(headers).forEach(function (key) { + lowHeaders[key.toLowerCase()] = headers[key] + }) + return lowHeaders } function hasHeader(options, headerKeyName) { @@ -67,10 +71,11 @@ function bodyToDataString(options, curlStringOptions) { // fall back to original body if it could not be parsed as JSON parsedData = options.body; } + const lowHeaders = getLowHeaders(options.headers) // return an ampersand delimited string if (hasHeader(options, 'content-type') && - getHeader(options, 'content-type').toLowerCase() === 'application/x-www-form-urlencoded') { + lowHeaders['content-type'].toLowerCase() === 'application/x-www-form-urlencoded') { if (typeof parsedData === 'string') { return parsedData; } else { From c6b897ed300ca6e4845a99749d0cce42087064cd Mon Sep 17 00:00:00 2001 From: liufuguo <595726017@qq.com> Date: Sat, 30 Jul 2022 09:12:01 +0800 Subject: [PATCH 2/2] match the style of this project --- index.js | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/index.js b/index.js index 46ca166..f5a80a5 100644 --- a/index.js +++ b/index.js @@ -43,12 +43,9 @@ function curlString( return curl; } -function getLowHeaders(headers) { - let lowHeaders = {} - Object.keys(headers).forEach(function (key) { - lowHeaders[key.toLowerCase()] = headers[key] - }) - return lowHeaders +function getHeader(options, headerKeyName) { + // return header that matches case, but if not found fall back to header that does not match case + return options.headers[headerKeyName] || options.headers[Object.keys(options.headers).find(key => key.toLowerCase() === headerKeyName.toLowerCase())] } function hasHeader(options, headerKeyName) { @@ -71,11 +68,10 @@ function bodyToDataString(options, curlStringOptions) { // fall back to original body if it could not be parsed as JSON parsedData = options.body; } - const lowHeaders = getLowHeaders(options.headers) // return an ampersand delimited string if (hasHeader(options, 'content-type') && - lowHeaders['content-type'].toLowerCase() === 'application/x-www-form-urlencoded') { + getHeader(options, 'content-type').toLowerCase() === 'application/x-www-form-urlencoded') { if (typeof parsedData === 'string') { return parsedData; } else {