简体   繁体   中英

How to escape double quotes and exclamation mark in password?

I have the following code:

curl -s --insecure -H "Content-Type: application/json" -X POST -d "{\"username\":\"$1\",\"password\":\"$2\"}" http://apiurl

In the above curl command I want to escape the " and ! in the password. Right now the password is just given as $2

I have modified the curl command as below but it doesn't seem to work

curl -s --insecure -H "Content-Type: application/json" -X POST -d "{\"username\":\"$1\",\"password\":\"$2\"}" http://apiurl

Do not try to generate JSON manually; use a program like jq , which knows how to escape things properly, to generate it for you.

json=$(jq -n --arg u "$1" --arg p "$2" '{username: $u, password: $p}')
curl -s --insecure -H "Content-Type: application/json" -X POST -d "$json" http://apiurl

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM