Skip to content

Conversation

@erunion
Copy link
Contributor

@erunion erunion commented May 13, 2021

This slightly modifies the cURL snippet target to clean up how it handles JSON payloads into rendering them on multiple lines.

This is what they currently look like:

curl --request POST \
  --url http://mockbin.com/har \
  --header 'content-type: application/json' \
  --data '{"number":1,"string":"f\"oo","arr":[1,2,3],"nested":{"a":"b"},"arr_mix":[1,"a",{"arr_mix_nested":{}}],"boolean":false}'

With this change:

curl --request POST \
  --url http://mockbin.com/har \
  --header 'content-type: application/json' \
  --data '
{
  "number": 1,
  "string": "f\"oo",
  "arr": [
    1,
    2,
    3
  ],
  "nested": {
    "a": "b"
  },
  "arr_mix": [
    1,
    "a",
    {
      "arr_mix_nested": {}
    }
  ],
  "boolean": false
}
'

🐳 Things to note

  • I don't love how arrays and objects get blown out if they're small enough to be able to fit on a single line (like arr is above), but the stringify-object library that's used throughout this library for doing prettification of objects removes quotes from object keys, which in this case would result in invalid JSON from being sent.
  • If the JSON payload has a ' quote within it, we can't wrap that in a --data string without busting STDIN so instead we wrap the JSON within a HEREDOC:
curl --request POST \
  --url http://mockbin.com/har \
  --header 'content-type: application/json' \
  --data @- <<EOF
{
  "number": 1,
  "string": "f'oo"
}
EOF
  • To prevent miniscule JSON payloads from getting blown out snippets, if the stringified JSON is less than or equal to 20 characters we'll render it on one line:
curl --request POST \
  --url http://mockbin.com/har \
  --header 'content-type: application/json' \
  --data '{"foo":null}'

@reynolek
Copy link
Contributor

reynolek commented Sep 3, 2021

@erunion I was going to merge this for the next release, but with the conflicts I didnt feel confident fixing them up for you (happened due to the change from #227)

Mind fixing it up and ill merge it later.

@erunion
Copy link
Contributor Author

erunion commented Sep 7, 2021

@reynolek I just fixed the merge conflict so this should be good to go after Travis gives it the 👍

'text/json',
'text/x-json',
'+json'
].some(function (type) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NOTE: you could condense lines 53-55 to:

      ].some(mimeType.includes.bind(mimeType))

@erunion erunion closed this Apr 29, 2022
@erunion erunion deleted the feat/cleaner-curl-json branch April 29, 2022 22:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants