From eebebaef64c706734d2c2c8bfc095ba886278d71 Mon Sep 17 00:00:00 2001 From: Hugo <95517615+HugoHSun@users.noreply.github.com> Date: Tue, 17 Feb 2026 19:33:28 +1100 Subject: [PATCH] fix: use --data-urlencode for form-urlencoded curl snippets --- src/targets/shell/curl/client.test.ts | 15 +++++++++++++++ src/targets/shell/curl/client.ts | 6 ++---- .../curl/fixtures/application-form-encoded.sh | 4 ++-- .../shell/curl/fixtures/custom-indentation.sh | 2 +- src/targets/shell/curl/fixtures/full.sh | 2 +- src/targets/shell/curl/fixtures/short-options.sh | 2 +- .../shell/curl/fixtures/urlencode-values.sh | 5 +++++ 7 files changed, 27 insertions(+), 9 deletions(-) create mode 100644 src/targets/shell/curl/fixtures/urlencode-values.sh diff --git a/src/targets/shell/curl/client.test.ts b/src/targets/shell/curl/client.test.ts index 7713ecde4..d0cf3b742 100644 --- a/src/targets/shell/curl/client.test.ts +++ b/src/targets/shell/curl/client.test.ts @@ -88,6 +88,21 @@ runCustomFixtures({ options: {}, expected: 'urlencode.sh', }, + { + it: 'should use --data-urlencode for form-urlencoded params with special characters in values', + input: { + ...applicationFormEncoded.log.entries[0].request, + postData: { + mimeType: 'application/x-www-form-urlencoded', + params: [ + { name: 'query', value: 'hello world' }, + { name: 'filter', value: 'status=active&type=user' }, + ], + }, + } as Request, + options: {}, + expected: 'urlencode-values.sh', + }, { it: 'should send JSON-encoded data with single quotes within a HEREDOC', input: { diff --git a/src/targets/shell/curl/client.ts b/src/targets/shell/curl/client.ts index 78656fbab..2dee8cfcd 100644 --- a/src/targets/shell/curl/client.ts +++ b/src/targets/shell/curl/client.ts @@ -138,11 +138,9 @@ export const curl: Client = { case 'application/x-www-form-urlencoded': if (postData.params) { postData.params.forEach(param => { - const unencoded = param.name; const encoded = encodeURIComponent(param.name); - const needsEncoding = encoded !== unencoded; - const name = needsEncoding ? encoded : unencoded; - const flag = binary ? '--data-binary' : needsEncoding ? '--data-urlencode' : arg('data'); + const name = encoded !== param.name ? encoded : param.name; + const flag = binary ? '--data-binary' : '--data-urlencode'; push(`${flag} ${quote(`${name}=${param.value}`)}`); }); } else { diff --git a/src/targets/shell/curl/fixtures/application-form-encoded.sh b/src/targets/shell/curl/fixtures/application-form-encoded.sh index 1415f4531..8edd1eff3 100755 --- a/src/targets/shell/curl/fixtures/application-form-encoded.sh +++ b/src/targets/shell/curl/fixtures/application-form-encoded.sh @@ -1,5 +1,5 @@ curl --request POST \ --url https://httpbin.org/anything \ --header 'content-type: application/x-www-form-urlencoded' \ - --data foo=bar \ - --data hello=world \ No newline at end of file + --data-urlencode foo=bar \ + --data-urlencode hello=world \ No newline at end of file diff --git a/src/targets/shell/curl/fixtures/custom-indentation.sh b/src/targets/shell/curl/fixtures/custom-indentation.sh index ce831a519..9146c1b61 100755 --- a/src/targets/shell/curl/fixtures/custom-indentation.sh +++ b/src/targets/shell/curl/fixtures/custom-indentation.sh @@ -3,4 +3,4 @@ curl --request POST \ @--header 'accept: application/json' \ @--header 'content-type: application/x-www-form-urlencoded' \ @--cookie 'foo=bar; bar=baz' \ -@--data foo=bar \ No newline at end of file +@--data-urlencode foo=bar \ No newline at end of file diff --git a/src/targets/shell/curl/fixtures/full.sh b/src/targets/shell/curl/fixtures/full.sh index b28d45181..892866ae8 100755 --- a/src/targets/shell/curl/fixtures/full.sh +++ b/src/targets/shell/curl/fixtures/full.sh @@ -3,4 +3,4 @@ curl --request POST \ --header 'accept: application/json' \ --header 'content-type: application/x-www-form-urlencoded' \ --cookie 'foo=bar; bar=baz' \ - --data foo=bar \ No newline at end of file + --data-urlencode foo=bar \ No newline at end of file diff --git a/src/targets/shell/curl/fixtures/short-options.sh b/src/targets/shell/curl/fixtures/short-options.sh index 6f123f22f..09604ace3 100755 --- a/src/targets/shell/curl/fixtures/short-options.sh +++ b/src/targets/shell/curl/fixtures/short-options.sh @@ -1 +1 @@ -curl -X POST 'https://httpbin.org/anything?foo=bar&foo=baz&baz=abc&key=value' -H 'accept: application/json' -H 'content-type: application/x-www-form-urlencoded' -b 'foo=bar; bar=baz' -d foo=bar \ No newline at end of file +curl -X POST 'https://httpbin.org/anything?foo=bar&foo=baz&baz=abc&key=value' -H 'accept: application/json' -H 'content-type: application/x-www-form-urlencoded' -b 'foo=bar; bar=baz' --data-urlencode foo=bar \ No newline at end of file diff --git a/src/targets/shell/curl/fixtures/urlencode-values.sh b/src/targets/shell/curl/fixtures/urlencode-values.sh new file mode 100644 index 000000000..c3b2e2b26 --- /dev/null +++ b/src/targets/shell/curl/fixtures/urlencode-values.sh @@ -0,0 +1,5 @@ +curl --request POST \ + --url https://httpbin.org/anything \ + --header 'content-type: application/x-www-form-urlencoded' \ + --data-urlencode 'query=hello world' \ + --data-urlencode 'filter=status=active&type=user' \ No newline at end of file