Created
February 4, 2025 12:35
-
-
Save nilleb/5503d67d5e62400c75a8c238546101d2 to your computer and use it in GitHub Desktop.
compare `qs.stringify` and `axios post with a dict` (using content-type `application/x-www-form-urlencoded`)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const axios = require('axios') | |
const qs = require('qs') | |
const url = 'https://httpbin.org/post' | |
const clientId = '9495d6bb-41c2-4c58-848f-92e52cf3d640' | |
const clientSecret = '9495d6bb-41c2-4c58-848f-92e52cf3d640' | |
async function stringify(resource) { | |
let params = { | |
grant_type: 'client_credentials', | |
client_id: `${clientId}`, | |
client_secret: `${clientSecret}`, | |
resource | |
} | |
const body = qs.stringify(params) | |
const response = await axios.post(url, body, { | |
headers: { | |
'Content-Type': 'application/x-www-form-urlencoded' | |
}, | |
}); | |
return response | |
}; | |
async function pureAxios(resource) { | |
let params = { | |
grant_type: 'client_credentials', | |
client_id: `${clientId}`, | |
client_secret: `${clientSecret}`, | |
resource | |
} | |
const body = params; | |
const response = await axios.post(url, body, { | |
headers: { | |
'Content-Type': 'application/x-www-form-urlencoded' | |
} | |
}) | |
return response | |
} | |
async function compare() { | |
const resource = 'https://management.azure.com/.default' | |
const stringifyResponse = await stringify(resource) | |
const pureAxiosResponse = await pureAxios(resource) | |
const leftData = stringifyResponse.data | |
delete leftData.headers['X-Amzn-Trace-Id'] | |
const rightData = pureAxiosResponse.data | |
delete rightData.headers['X-Amzn-Trace-Id'] | |
const left = JSON.stringify(leftData) | |
const right = JSON.stringify(rightData) | |
let differs = false | |
for (let i = 0; i < Math.max(left.length, right.length); i++) { | |
if (left[i] !== right[i]) { | |
console.log(i) | |
console.log(left.slice(i - 3, i + 3)) | |
console.log(right.slice(i - 3, i + 3)) | |
differs = true | |
} | |
} | |
if (!differs) { | |
console.log('No differences') | |
} | |
} | |
async function main() { | |
await compare() | |
} | |
main() | |
module.exports = { | |
stringify, | |
pureAxios, | |
compare | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "test-axios", | |
"version": "1.0.0", | |
"lockfileVersion": 3, | |
"requires": true, | |
"packages": { | |
"": { | |
"name": "test-axios", | |
"version": "1.0.0", | |
"license": "ISC", | |
"dependencies": { | |
"axios": "^1.7.9", | |
"qs": "^6.14.0" | |
} | |
}, | |
"node_modules/asynckit": { | |
"version": "0.4.0", | |
"resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz", | |
"integrity": "sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==", | |
"license": "MIT" | |
}, | |
"node_modules/axios": { | |
"version": "1.7.9", | |
"resolved": "https://registry.npmjs.org/axios/-/axios-1.7.9.tgz", | |
"integrity": "sha512-LhLcE7Hbiryz8oMDdDptSrWowmB4Bl6RCt6sIJKpRB4XtVf0iEgewX3au/pJqm+Py1kCASkb/FFKjxQaLtxJvw==", | |
"license": "MIT", | |
"dependencies": { | |
"follow-redirects": "^1.15.6", | |
"form-data": "^4.0.0", | |
"proxy-from-env": "^1.1.0" | |
} | |
}, | |
"node_modules/call-bind-apply-helpers": { | |
"version": "1.0.1", | |
"resolved": "https://registry.npmjs.org/call-bind-apply-helpers/-/call-bind-apply-helpers-1.0.1.tgz", | |
"integrity": "sha512-BhYE+WDaywFg2TBWYNXAE+8B1ATnThNBqXHP5nQu0jWJdVvY2hvkpyB3qOmtmDePiS5/BDQ8wASEWGMWRG148g==", | |
"license": "MIT", | |
"dependencies": { | |
"es-errors": "^1.3.0", | |
"function-bind": "^1.1.2" | |
}, | |
"engines": { | |
"node": ">= 0.4" | |
} | |
}, | |
"node_modules/call-bound": { | |
"version": "1.0.3", | |
"resolved": "https://registry.npmjs.org/call-bound/-/call-bound-1.0.3.tgz", | |
"integrity": "sha512-YTd+6wGlNlPxSuri7Y6X8tY2dmm12UMH66RpKMhiX6rsk5wXXnYgbUcOt8kiS31/AjfoTOvCsE+w8nZQLQnzHA==", | |
"license": "MIT", | |
"dependencies": { | |
"call-bind-apply-helpers": "^1.0.1", | |
"get-intrinsic": "^1.2.6" | |
}, | |
"engines": { | |
"node": ">= 0.4" | |
}, | |
"funding": { | |
"url": "https://github.com/sponsors/ljharb" | |
} | |
}, | |
"node_modules/combined-stream": { | |
"version": "1.0.8", | |
"resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.8.tgz", | |
"integrity": "sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==", | |
"license": "MIT", | |
"dependencies": { | |
"delayed-stream": "~1.0.0" | |
}, | |
"engines": { | |
"node": ">= 0.8" | |
} | |
}, | |
"node_modules/delayed-stream": { | |
"version": "1.0.0", | |
"resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz", | |
"integrity": "sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ==", | |
"license": "MIT", | |
"engines": { | |
"node": ">=0.4.0" | |
} | |
}, | |
"node_modules/dunder-proto": { | |
"version": "1.0.1", | |
"resolved": "https://registry.npmjs.org/dunder-proto/-/dunder-proto-1.0.1.tgz", | |
"integrity": "sha512-KIN/nDJBQRcXw0MLVhZE9iQHmG68qAVIBg9CqmUYjmQIhgij9U5MFvrqkUL5FbtyyzZuOeOt0zdeRe4UY7ct+A==", | |
"license": "MIT", | |
"dependencies": { | |
"call-bind-apply-helpers": "^1.0.1", | |
"es-errors": "^1.3.0", | |
"gopd": "^1.2.0" | |
}, | |
"engines": { | |
"node": ">= 0.4" | |
} | |
}, | |
"node_modules/es-define-property": { | |
"version": "1.0.1", | |
"resolved": "https://registry.npmjs.org/es-define-property/-/es-define-property-1.0.1.tgz", | |
"integrity": "sha512-e3nRfgfUZ4rNGL232gUgX06QNyyez04KdjFrF+LTRoOXmrOgFKDg4BCdsjW8EnT69eqdYGmRpJwiPVYNrCaW3g==", | |
"license": "MIT", | |
"engines": { | |
"node": ">= 0.4" | |
} | |
}, | |
"node_modules/es-errors": { | |
"version": "1.3.0", | |
"resolved": "https://registry.npmjs.org/es-errors/-/es-errors-1.3.0.tgz", | |
"integrity": "sha512-Zf5H2Kxt2xjTvbJvP2ZWLEICxA6j+hAmMzIlypy4xcBg1vKVnx89Wy0GbS+kf5cwCVFFzdCFh2XSCFNULS6csw==", | |
"license": "MIT", | |
"engines": { | |
"node": ">= 0.4" | |
} | |
}, | |
"node_modules/es-object-atoms": { | |
"version": "1.1.1", | |
"resolved": "https://registry.npmjs.org/es-object-atoms/-/es-object-atoms-1.1.1.tgz", | |
"integrity": "sha512-FGgH2h8zKNim9ljj7dankFPcICIK9Cp5bm+c2gQSYePhpaG5+esrLODihIorn+Pe6FGJzWhXQotPv73jTaldXA==", | |
"license": "MIT", | |
"dependencies": { | |
"es-errors": "^1.3.0" | |
}, | |
"engines": { | |
"node": ">= 0.4" | |
} | |
}, | |
"node_modules/follow-redirects": { | |
"version": "1.15.9", | |
"resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.9.tgz", | |
"integrity": "sha512-gew4GsXizNgdoRyqmyfMHyAmXsZDk6mHkSxZFCzW9gwlbtOW44CDtYavM+y+72qD/Vq2l550kMF52DT8fOLJqQ==", | |
"funding": [ | |
{ | |
"type": "individual", | |
"url": "https://github.com/sponsors/RubenVerborgh" | |
} | |
], | |
"license": "MIT", | |
"engines": { | |
"node": ">=4.0" | |
}, | |
"peerDependenciesMeta": { | |
"debug": { | |
"optional": true | |
} | |
} | |
}, | |
"node_modules/form-data": { | |
"version": "4.0.1", | |
"resolved": "https://registry.npmjs.org/form-data/-/form-data-4.0.1.tgz", | |
"integrity": "sha512-tzN8e4TX8+kkxGPK8D5u0FNmjPUjw3lwC9lSLxxoB/+GtsJG91CO8bSWy73APlgAZzZbXEYZJuxjkHH2w+Ezhw==", | |
"license": "MIT", | |
"dependencies": { | |
"asynckit": "^0.4.0", | |
"combined-stream": "^1.0.8", | |
"mime-types": "^2.1.12" | |
}, | |
"engines": { | |
"node": ">= 6" | |
} | |
}, | |
"node_modules/function-bind": { | |
"version": "1.1.2", | |
"resolved": "https://registry.npmjs.org/function-bind/-/function-bind-1.1.2.tgz", | |
"integrity": "sha512-7XHNxH7qX9xG5mIwxkhumTox/MIRNcOgDrxWsMt2pAr23WHp6MrRlN7FBSFpCpr+oVO0F744iUgR82nJMfG2SA==", | |
"license": "MIT", | |
"funding": { | |
"url": "https://github.com/sponsors/ljharb" | |
} | |
}, | |
"node_modules/get-intrinsic": { | |
"version": "1.2.7", | |
"resolved": "https://registry.npmjs.org/get-intrinsic/-/get-intrinsic-1.2.7.tgz", | |
"integrity": "sha512-VW6Pxhsrk0KAOqs3WEd0klDiF/+V7gQOpAvY1jVU/LHmaD/kQO4523aiJuikX/QAKYiW6x8Jh+RJej1almdtCA==", | |
"license": "MIT", | |
"dependencies": { | |
"call-bind-apply-helpers": "^1.0.1", | |
"es-define-property": "^1.0.1", | |
"es-errors": "^1.3.0", | |
"es-object-atoms": "^1.0.0", | |
"function-bind": "^1.1.2", | |
"get-proto": "^1.0.0", | |
"gopd": "^1.2.0", | |
"has-symbols": "^1.1.0", | |
"hasown": "^2.0.2", | |
"math-intrinsics": "^1.1.0" | |
}, | |
"engines": { | |
"node": ">= 0.4" | |
}, | |
"funding": { | |
"url": "https://github.com/sponsors/ljharb" | |
} | |
}, | |
"node_modules/get-proto": { | |
"version": "1.0.1", | |
"resolved": "https://registry.npmjs.org/get-proto/-/get-proto-1.0.1.tgz", | |
"integrity": "sha512-sTSfBjoXBp89JvIKIefqw7U2CCebsc74kiY6awiGogKtoSGbgjYE/G/+l9sF3MWFPNc9IcoOC4ODfKHfxFmp0g==", | |
"license": "MIT", | |
"dependencies": { | |
"dunder-proto": "^1.0.1", | |
"es-object-atoms": "^1.0.0" | |
}, | |
"engines": { | |
"node": ">= 0.4" | |
} | |
}, | |
"node_modules/gopd": { | |
"version": "1.2.0", | |
"resolved": "https://registry.npmjs.org/gopd/-/gopd-1.2.0.tgz", | |
"integrity": "sha512-ZUKRh6/kUFoAiTAtTYPZJ3hw9wNxx+BIBOijnlG9PnrJsCcSjs1wyyD6vJpaYtgnzDrKYRSqf3OO6Rfa93xsRg==", | |
"license": "MIT", | |
"engines": { | |
"node": ">= 0.4" | |
}, | |
"funding": { | |
"url": "https://github.com/sponsors/ljharb" | |
} | |
}, | |
"node_modules/has-symbols": { | |
"version": "1.1.0", | |
"resolved": "https://registry.npmjs.org/has-symbols/-/has-symbols-1.1.0.tgz", | |
"integrity": "sha512-1cDNdwJ2Jaohmb3sg4OmKaMBwuC48sYni5HUw2DvsC8LjGTLK9h+eb1X6RyuOHe4hT0ULCW68iomhjUoKUqlPQ==", | |
"license": "MIT", | |
"engines": { | |
"node": ">= 0.4" | |
}, | |
"funding": { | |
"url": "https://github.com/sponsors/ljharb" | |
} | |
}, | |
"node_modules/hasown": { | |
"version": "2.0.2", | |
"resolved": "https://registry.npmjs.org/hasown/-/hasown-2.0.2.tgz", | |
"integrity": "sha512-0hJU9SCPvmMzIBdZFqNPXWa6dqh7WdH0cII9y+CyS8rG3nL48Bclra9HmKhVVUHyPWNH5Y7xDwAB7bfgSjkUMQ==", | |
"license": "MIT", | |
"dependencies": { | |
"function-bind": "^1.1.2" | |
}, | |
"engines": { | |
"node": ">= 0.4" | |
} | |
}, | |
"node_modules/math-intrinsics": { | |
"version": "1.1.0", | |
"resolved": "https://registry.npmjs.org/math-intrinsics/-/math-intrinsics-1.1.0.tgz", | |
"integrity": "sha512-/IXtbwEk5HTPyEwyKX6hGkYXxM9nbj64B+ilVJnC/R6B0pH5G4V3b0pVbL7DBj4tkhBAppbQUlf6F6Xl9LHu1g==", | |
"license": "MIT", | |
"engines": { | |
"node": ">= 0.4" | |
} | |
}, | |
"node_modules/mime-db": { | |
"version": "1.52.0", | |
"resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.52.0.tgz", | |
"integrity": "sha512-sPU4uV7dYlvtWJxwwxHD0PuihVNiE7TyAbQ5SWxDCB9mUYvOgroQOwYQQOKPJ8CIbE+1ETVlOoK1UC2nU3gYvg==", | |
"license": "MIT", | |
"engines": { | |
"node": ">= 0.6" | |
} | |
}, | |
"node_modules/mime-types": { | |
"version": "2.1.35", | |
"resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.35.tgz", | |
"integrity": "sha512-ZDY+bPm5zTTF+YpCrAU9nK0UgICYPT0QtT1NZWFv4s++TNkcgVaT0g6+4R2uI4MjQjzysHB1zxuWL50hzaeXiw==", | |
"license": "MIT", | |
"dependencies": { | |
"mime-db": "1.52.0" | |
}, | |
"engines": { | |
"node": ">= 0.6" | |
} | |
}, | |
"node_modules/object-inspect": { | |
"version": "1.13.3", | |
"resolved": "https://registry.npmjs.org/object-inspect/-/object-inspect-1.13.3.tgz", | |
"integrity": "sha512-kDCGIbxkDSXE3euJZZXzc6to7fCrKHNI/hSRQnRuQ+BWjFNzZwiFF8fj/6o2t2G9/jTj8PSIYTfCLelLZEeRpA==", | |
"license": "MIT", | |
"engines": { | |
"node": ">= 0.4" | |
}, | |
"funding": { | |
"url": "https://github.com/sponsors/ljharb" | |
} | |
}, | |
"node_modules/proxy-from-env": { | |
"version": "1.1.0", | |
"resolved": "https://registry.npmjs.org/proxy-from-env/-/proxy-from-env-1.1.0.tgz", | |
"integrity": "sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg==", | |
"license": "MIT" | |
}, | |
"node_modules/qs": { | |
"version": "6.14.0", | |
"resolved": "https://registry.npmjs.org/qs/-/qs-6.14.0.tgz", | |
"integrity": "sha512-YWWTjgABSKcvs/nWBi9PycY/JiPJqOD4JA6o9Sej2AtvSGarXxKC3OQSk4pAarbdQlKAh5D4FCQkJNkW+GAn3w==", | |
"license": "BSD-3-Clause", | |
"dependencies": { | |
"side-channel": "^1.1.0" | |
}, | |
"engines": { | |
"node": ">=0.6" | |
}, | |
"funding": { | |
"url": "https://github.com/sponsors/ljharb" | |
} | |
}, | |
"node_modules/side-channel": { | |
"version": "1.1.0", | |
"resolved": "https://registry.npmjs.org/side-channel/-/side-channel-1.1.0.tgz", | |
"integrity": "sha512-ZX99e6tRweoUXqR+VBrslhda51Nh5MTQwou5tnUDgbtyM0dBgmhEDtWGP/xbKn6hqfPRHujUNwz5fy/wbbhnpw==", | |
"license": "MIT", | |
"dependencies": { | |
"es-errors": "^1.3.0", | |
"object-inspect": "^1.13.3", | |
"side-channel-list": "^1.0.0", | |
"side-channel-map": "^1.0.1", | |
"side-channel-weakmap": "^1.0.2" | |
}, | |
"engines": { | |
"node": ">= 0.4" | |
}, | |
"funding": { | |
"url": "https://github.com/sponsors/ljharb" | |
} | |
}, | |
"node_modules/side-channel-list": { | |
"version": "1.0.0", | |
"resolved": "https://registry.npmjs.org/side-channel-list/-/side-channel-list-1.0.0.tgz", | |
"integrity": "sha512-FCLHtRD/gnpCiCHEiJLOwdmFP+wzCmDEkc9y7NsYxeF4u7Btsn1ZuwgwJGxImImHicJArLP4R0yX4c2KCrMrTA==", | |
"license": "MIT", | |
"dependencies": { | |
"es-errors": "^1.3.0", | |
"object-inspect": "^1.13.3" | |
}, | |
"engines": { | |
"node": ">= 0.4" | |
}, | |
"funding": { | |
"url": "https://github.com/sponsors/ljharb" | |
} | |
}, | |
"node_modules/side-channel-map": { | |
"version": "1.0.1", | |
"resolved": "https://registry.npmjs.org/side-channel-map/-/side-channel-map-1.0.1.tgz", | |
"integrity": "sha512-VCjCNfgMsby3tTdo02nbjtM/ewra6jPHmpThenkTYh8pG9ucZ/1P8So4u4FGBek/BjpOVsDCMoLA/iuBKIFXRA==", | |
"license": "MIT", | |
"dependencies": { | |
"call-bound": "^1.0.2", | |
"es-errors": "^1.3.0", | |
"get-intrinsic": "^1.2.5", | |
"object-inspect": "^1.13.3" | |
}, | |
"engines": { | |
"node": ">= 0.4" | |
}, | |
"funding": { | |
"url": "https://github.com/sponsors/ljharb" | |
} | |
}, | |
"node_modules/side-channel-weakmap": { | |
"version": "1.0.2", | |
"resolved": "https://registry.npmjs.org/side-channel-weakmap/-/side-channel-weakmap-1.0.2.tgz", | |
"integrity": "sha512-WPS/HvHQTYnHisLo9McqBHOJk2FkHO/tlpvldyrnem4aeQp4hai3gythswg6p01oSoTl58rcpiFAjF2br2Ak2A==", | |
"license": "MIT", | |
"dependencies": { | |
"call-bound": "^1.0.2", | |
"es-errors": "^1.3.0", | |
"get-intrinsic": "^1.2.5", | |
"object-inspect": "^1.13.3", | |
"side-channel-map": "^1.0.1" | |
}, | |
"engines": { | |
"node": ">= 0.4" | |
}, | |
"funding": { | |
"url": "https://github.com/sponsors/ljharb" | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "test-axios", | |
"version": "1.0.0", | |
"main": "index.js", | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"author": "", | |
"license": "ISC", | |
"description": "", | |
"dependencies": { | |
"axios": "^1.7.9", | |
"qs": "^6.14.0" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment