Home > database >  How to Format CURL Command with Quotes in Expect
How to Format CURL Command with Quotes in Expect

Time:07-30

I'm writing an expect script that sshes into a device then runs a curl command. My curl command looks like this:

curl -k -X POST --data '{"username": "exuser", "password": "expassword", "remember": "true"}' -c ~/Desktop/cookie.txt https://basecontroller_<url>:8443/api/login

To send this command using expect I'm writing a command like:

send "curl -k -X POST --data '{"username": "exuser", "password": "expassword", "remember": "true"}' -c ~/Desktop/cookie.txt https://basecontroller_<url>:8443/api/login"

The only difference is the quotations at the start and back of the command in the expect version. However, it's not working due to the double quotes being used in my --data block. I need to specify the username and password. Does anyone know how to get around this? I've tried using single quotes but they also don't work. Thanks for the help!

CodePudding user response:

Escaping the nested quotes fixed the issue, putting a backslach before the double quotes in the --data block.

Ex: '{\"username\": \"exuser\",...}'

CodePudding user response:

Indeed, you need escapes double quotes to use json format in raw curl cli. However, to translate easily your raw json inline:

→ cat << EOF > data.json
{"username": "exuser", "password": "expassword", "remember": "true"}
EOF

→ echo $(sed 's/"/\\"/g' data.json)
{\"username\": \"exuser\", \"password\": \"expassword\", \"remember\": \"true\"}

→ cat data.json | jq

{
  "username": "exuser",
  "password": "expassword",
  "remember": "true"
}

→ curl -k -X POST --data $(sed 's/"/\\"/g' data.json) https://basecontroller_<url>:8443/api/login

OR

→ sed -i 's/"/\\"/g' data.json

→ curl -k -X POST --data @data.json https://basecontroller_<url>:8443/api/login's/"/\\"/g' data.json
  • Related