Commit d1f7c12
authored
fix: use --data-urlencode for form-urlencoded curl snippets (#315)
| 🚥 Resolves CX-2935 |
| :------------------- |
## 🧰 Changes
Curl snippets for `application/x-www-form-urlencoded` endpoints were
using `--data` instead of `--data-urlencode`. This meant values with
special characters (spaces, ampersands, etc.) would break when
copy-paste and execute in a terminal.
The old code only checked if the param **name** needed encoding and
picked the flag based on that, while ignoring the value. The fix: always
use `--data-urlencode` for form-urlencoded params. It behaves
identically to `--data` when there's nothing to encode, so no downside.
Note that the form data values are never pre-encoded though, since
`oas-to-har` just calls `String()` on them.
This only affects the code snippet shown to developers, the "Try It"
executor uses `fetch-har` with `URLSearchParams` which already encodes
correctly.
## 🧬 QA & Testing
Find an endpoint with `application/x-www-form-urlencoded` content type,
check that the curl snippet uses `--data-urlencode` instead of `--data`
### Correct Example
```zsh
curl --request POST \
--url https://httpbin.org/anything \
--header 'content-type: application/x-www-form-urlencoded' \
--data-urlencode 'foo=bar' \
--data-urlencode 'query=hello world' \
--data-urlencode 'filter=status=active&type=user'
```
```json
{
"args": {},
"data": "",
"files": {},
"form": {
"filter": "status=active&type=user",
"foo": "bar",
"query": "hello world"
},
"headers": {
"Accept": "*/*",
"Content-Length": "62",
"Content-Type": "application/x-www-form-urlencoded",
"Host": "httpbin.org",
"User-Agent": "curl/8.7.1",
"X-Amzn-Trace-Id": "Root=1-69942c21-2119bf28086765883dfaa673"
},
"json": null,
"method": "POST",
"origin": "1.1.1.1",
"url": "https://httpbin.org/anything"
}
```
### Incorrect Example
```zsh
curl --request POST \
--url https://httpbin.org/anything \
--header 'content-type: application/x-www-form-urlencoded' \
--data 'foo=bar' \
--data 'query=hello world' \
--data 'filter=status=active&type=user'
```
```json
{
"args": {},
"data": "",
"files": {},
"form": {
"filter": "status=active",
"foo": "bar",
"query": "hello world",
"type": "user"
},
"headers": {
"Accept": "*/*",
"Content-Length": "56",
"Content-Type": "application/x-www-form-urlencoded",
"Host": "httpbin.org",
"User-Agent": "curl/8.7.1",
"X-Amzn-Trace-Id": "Root=1-69942d09-4ad6930b09907b011e840bec"
},
"json": null,
"method": "POST",
"origin": "1.1.1.1",
"url": "https://httpbin.org/anything"
}
```1 parent ae49e9f commit d1f7c12
7 files changed
Lines changed: 27 additions & 9 deletions
File tree
- src/targets/shell/curl
- fixtures
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
88 | 88 | | |
89 | 89 | | |
90 | 90 | | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
91 | 106 | | |
92 | 107 | | |
93 | 108 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
138 | 138 | | |
139 | 139 | | |
140 | 140 | | |
141 | | - | |
142 | 141 | | |
143 | | - | |
144 | | - | |
145 | | - | |
| 142 | + | |
| 143 | + | |
146 | 144 | | |
147 | 145 | | |
148 | 146 | | |
| |||
Lines changed: 2 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
4 | | - | |
5 | | - | |
| 4 | + | |
| 5 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
6 | | - | |
| 6 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
6 | | - | |
| 6 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
| 1 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
0 commit comments