4

I'm using CURL to send JSON data. Here is an example:

mycomputer$ curl -H "Content-Type: application/json" 
     -d  "{ "some_string": "Hello mom it's me!" }"
     "http://localhost:3001/api_v2/blocks/42af6ab04d9d9635a97f8abec14ed023?api_key=fe5cf0d86af27c086ab5cd4d0eab6641"

How can I escape the contents of any value for some_string?

For example if someone wants to put in the string Abe Lincoln's favorite character is the backslash \. He said "I love the \ and single quotes like ''". how can I escape this while using curl?

I think I need to do the following:

  • If the string contains a " escape it with a triple backslash \\\".

  • If the string contains a ' escape it doesn't need escaping '.

  • If the string contains a \ escape it with a triple backslash \\\\.

Are there any characters I am forgetting?

3

I'm going to assume your objective is just getting the string past the shell's parsing. If so, Use read:

For example

$ IFS='' read -r var

Then manually paste the line.

If it's multiple lines you can use:

$ IFS='' read -r -d '' var

And paste again, but this time use CTRL+d to end the input.

Or use a heredoc:

$ IFS='' read -r -d '' var <<'EOF'
{ "some_string": "Hello mom it's me!" }
EOF

 

Whichever method you use, you can then just use the variable $var for accessing it:

$ curl -H "Content-Type: application/json" \
 -d  "$var" \
 "http://localhost:3001/api_v2/blocks/42af6ab04d9d9635a97f8abec14ed023?api_key=fe5cf0d86af27c086ab5cd4d0eab6641"
1

You probably wouldn't need 3 of those backslashes if you can remove the shell from the equation. Luckily, you can:

curl --config - <<\DATA
    url    = "http://some.url"
    header = "Content-Type: application/json"
    data   = "{ \"some_string\": "Abe Lincoln's favorite character is the backslash \\. He said \"I love the \\ and single quotes like '.\""
#END
DATA

Look at man curl. I suggest paying close attention to the differences between --data-ascii (which is what -d means), --data-binary, and --data-urlencoded.

You might also want to compare the different behaviors you get with --header, --data, and --form (which, by default, all do a POST in slightly different ways), and what you might get with --get which can instead encode the data specified with any of the previously mentioned into the request url following a ?.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.