From 161b5d1ef662397ef690d0c9b7c32b350d02b6bb Mon Sep 17 00:00:00 2001 From: killagu Date: Tue, 20 Feb 2024 17:34:37 +0800 Subject: [PATCH 1/2] fix: use querystring to stringify data --- src/HttpClient.ts | 16 +++++++--------- test/options.data.test.ts | 3 ++- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/src/HttpClient.ts b/src/HttpClient.ts index 9ddd1bcb..7970e332 100644 --- a/src/HttpClient.ts +++ b/src/HttpClient.ts @@ -16,6 +16,7 @@ import { basename } from 'node:path'; import { createReadStream } from 'node:fs'; import { format as urlFormat } from 'node:url'; import { performance } from 'node:perf_hooks'; +import querystring from 'node:querystring'; import { FormData as FormDataNative, request as undiciRequest, @@ -503,18 +504,15 @@ export class HttpClient extends EventEmitter { || isReadable(args.data); if (isGETOrHEAD) { if (!isStringOrBufferOrReadable) { + let query; if (args.nestedQuerystring) { - const querystring = qs.stringify(args.data); - // reset the requestUrl - const href = requestUrl.href; - requestUrl = new URL(href + (href.includes('?') ? '&' : '?') + querystring); + query = qs.stringify(args.data); } else { - for (const field in args.data) { - const fieldValue = args.data[field]; - if (fieldValue === undefined) continue; - requestUrl.searchParams.append(field, fieldValue); - } + query = querystring.stringify(args.data); } + // reset the requestUrl + const href = requestUrl.href; + requestUrl = new URL(href + (href.includes('?') ? '&' : '?') + query); } } else { if (isStringOrBufferOrReadable) { diff --git a/test/options.data.test.ts b/test/options.data.test.ts index 6e7e65ef..a7dc15d7 100644 --- a/test/options.data.test.ts +++ b/test/options.data.test.ts @@ -29,6 +29,7 @@ describe('options.data.test.ts', () => { d: 1111, e() { return ''; }, f: true, + g: [ 'a', 'b' ], }, dataType: 'json', }); @@ -37,7 +38,7 @@ describe('options.data.test.ts', () => { assert.equal(response.data.method, 'GET'); assert(response.url.startsWith(_url)); assert(!response.redirected); - assert.equal(response.data.url, '/?sql=SELECT+*+from+table&data=%E5%93%88%E5%93%88&c=2222&d=1111&e=e%28%29+%7B%0A++++++++++return+%22%22%3B%0A++++++++%7D&f=true'); + assert.equal(response.data.url, '/?sql=SELECT+*+from+table&data=%E5%93%88%E5%93%88&c=2222&d=1111&e=e%28%29+%7B%0A++++++++++return+%22%22%3B%0A++++++++%7D&f=true&g=a&g=b'); const url = new URL(response.data.href); assert.equal(url.searchParams.get('sql'), 'SELECT * from table'); assert.equal(url.searchParams.get('data'), '哈哈'); From bc1a13fdfda26de0518dd84dcf4850c11a4fa1a5 Mon Sep 17 00:00:00 2001 From: killagu Date: Tue, 20 Feb 2024 17:43:38 +0800 Subject: [PATCH 2/2] f --- test/options.data.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/options.data.test.ts b/test/options.data.test.ts index a7dc15d7..f804bc7f 100644 --- a/test/options.data.test.ts +++ b/test/options.data.test.ts @@ -38,7 +38,7 @@ describe('options.data.test.ts', () => { assert.equal(response.data.method, 'GET'); assert(response.url.startsWith(_url)); assert(!response.redirected); - assert.equal(response.data.url, '/?sql=SELECT+*+from+table&data=%E5%93%88%E5%93%88&c=2222&d=1111&e=e%28%29+%7B%0A++++++++++return+%22%22%3B%0A++++++++%7D&f=true&g=a&g=b'); + assert.equal(response.data.url, '/?sql=SELECT%20*%20from%20table&data=%E5%93%88%E5%93%88&b=&c=2222&d=1111&e=&f=true&g=a&g=b'); const url = new URL(response.data.href); assert.equal(url.searchParams.get('sql'), 'SELECT * from table'); assert.equal(url.searchParams.get('data'), '哈哈'); @@ -165,7 +165,7 @@ describe('options.data.test.ts', () => { assert(response.url.startsWith(_url)); assert(!response.redirected); // console.log(response.data); - assert.equal(response.data.url, '/?that=in_path&sql=SELECT+*+from+table&data=%E5%93%88%E5%93%88'); + assert.equal(response.data.url, '/?that=in_path&sql=SELECT%20*%20from%20table&data=%E5%93%88%E5%93%88'); const url = new URL(response.data.href); assert.equal(url.searchParams.get('sql'), 'SELECT * from table'); assert.equal(url.searchParams.get('data'), '哈哈');