Unterminated quoted string in Capistrano curl command - ruby

I've tried just about every combination of single quotes, double quotes and backslashes that I can think of. If anyone could please point out what I'm doing wrong that'd be greatly appreciated.
run_locally do
execute "\\curl -X POST \"https://api.cloudflare.com/client/v4/zones/#{fetch(:cloudflare_zone)}/purge_cache\" \
-H \"X-Auth-Email: test#example.com\" \
-H \"X-Auth-Key: #{fetch(:cloudflare_api)}\" \
-H \"Content-Type: application/json\" \
--data {\"purge_everything\":true}\""
end
My capistrano script dies when it hits this every time. I feel like it's that last line but I'm not sure why.
Edit: I've gotten past that error, but now get "Malformed JSON in request body" back from Cloudflare.
run_locally do
execute "\\curl -X POST \"https://api.cloudflare.com/client/v4/zones/#{fetch(:cloudflare_zone)}/purge_cache\" \
-H \"X-Auth-Email: test#example.com\" \
-H \"X-Auth-Key: #{fetch(:cloudflare_api)}\" \
-H \"Content-Type: application/json\" \
--data \"{\"purge_everything\":true}\" "
end
So for some reason it doesn't like my --data section.

I'm not able to check this directly for you, but you can use other string constructs to do this in a clearer way which will hopefully highlight the issue for you.
run_locally do
execute %{curl -X POST "https://api.cloudflare.com/client/v4/zones/#{fetch(:cloudflare_zone)}/purge_cache" \
-H "X-Auth-Email: test#example.com" \
-H "X-Auth-Key: #{fetch(:cloudflare_api)}" \
-H "Content-Type: application/json" \
--data '{"purge_everything":true}'}
end
It's probably the nested double quotes in your JSON causing the issue.

Related

Trying to allow dynamic bash var for the prompt but escape char not working any ideas?

Trying to allow dynamic bash var for the prompt but escape char not working any ideas?
output=$(curl https://api.openai.com/v1/images/generations \
-H 'Content-Type: application/json' \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-d '{
"prompt": "$prompt",
"n": 1,
"size": "1024x1024"
}'
Tried escaping char but get error please help me allow bash vars in curl request.
The single quotes around the '-d' (--data) values are preventing the expansion of the variable.
Note that we are escaping the other double quotes in the json so they show up when the command is run. I assume you wanted them there, hence the single quotes.
You can do something along these lines (I like using the long version of options to show what the arguments are doing in a script).
$ prompt="A cute baby sea otter"
$ OPENAI_API_KEY=S0MeCo0lk3y
$ output=$(curl https://api.openai.com/v1/images/generations \
--header "Content-Type:application/json" \
--header "Authorization: Bearer ${OPENAI_API_KEY}" \
--data "{ \"prompt\":\"${prompt}\",\"n\": 1,\"size\": \"1024x1024\"}")
That sends the following:
curl https://api.openai.com/v1/images/generations --header Content-Type:application/json --header 'Authorization: Bearer S0MeCo0lk3y' --data '{ "prompt":"A cute baby sea otter","n": 1,"size": "1024x1024"}'
Checkout https://mywiki.wooledge.org/BashFAQ/050 as it might help with what you are trying to do.

Sending cURL data to multiple URLs

My code consists of the following:
curl -k -X DELETE \
-H "X-Parse-Application-Id: staticid" \
-H "X-Parse-Master-Key: statickey" \
https://mystatic.url/dynamicvalue
I have a list of URLS:
https://mystatic.url/johndylan
https://mystatic.url/marypoppins
etc, included in a tobedeleted.txt file and I would like to modify my cURL code to be something like this (which I've tried but didnt work:
curl -k -X DELETE \
-H "X-Parse-Application-Id: staticid" \
-H "X-Parse-Master-Key: statickey" \
> tobedeleted.txt
or to something like this (which also I've tried but it didnt worked)
curl -k -X DELETE \
-H "X-Parse-Application-Id: staticid" \
-H "X-Parse-Master-Key: statickey" \
https://mystatic.url/$tobedeleted.txt
Note that i want to run the same cURL command, each time for each line of the file, so I guess that I would need something like foreach function, since this is a bash script.
You are going to want something like this:
while read value
do
curl -k -X DELETE \
-H "X-Parse-Application-Id: staticid" \
-H "X-Parse-Master-Key: statickey" \
https://mystatic.url/$value
done < tobedeleted.txt

Nested cURL call

I've one cron task to update my DDNS with my current ip address and do it through a cURL call.
The problem is one of the parameters to pass in the call is the CURRENT IP and in order to discover ir i need to do another cURL call.
I would like to know if is possible to nest two cURL calls in one single script in order to make my cron task avoiding extra scripts
example:
to get my current ip I use
curl ipinfo.io/ip
to update my ddns i need to do:
curl -X PUT "https://api.cloudflare.com/client/v4/zones/2wertyh/dns_records/23ertghj" \
-H "X-Auth-Email: tomatechines#gmail.com" \
-H "X-Auth-Key: 123ertgyh" \
-H "Content-Type: application/json" \
--data '{"type":"A","name":"qwsdfg.com.br","content":"MY-CURRENT-IP","ttl":1800,"proxied":false}'
how can i fit this two calls together in order to make my cron task
Use command substitution, like this:
curl -X PUT "https://api.cloudflare.com/client/v4/zones/2wertyh/dns_records/23ertghj" \
-H "X-Auth-Email: tomatechines#gmail.com" \
-H "X-Auth-Key: 123ertgyh" \
-H "Content-Type: application/json" \
--data '{"type":"A","name":"qwsdfg.com.br","content":"'"$(curl ipinfo.io/ip)"'","ttl":1800,"proxied":false}'
String argument for --data is composed from three concatenated parts, 'beginning' "$(curl ...)" 'ending' (more details see in this answer).

How to send multiple words in a curl post message

I am using below code to Post a curl command. But it is not taking any space or line-break as message input.
I tried with %20 and other answers that is already in SO regarding this problem. None is working. It is giving error as
"$error":"Unexpected end-of-input: was expecting closing quote for a string value\n
line="abc def"
curl --user "USER":"Password" -H "Content-Type: application/json" -X POST -d '{"message":"'${line}'"}}' --url http://${host}:${port}${REST_URL}
There is a an unnecessary extra close brace } for your data segment. Also, the variables in the middle of the data argument should be quoted. Also double-quote your --url string to prevent word-splitting by the shell.
curl --user "USER":"Password" \
-H "Content-Type: application/json" \
-X POST -d '{"message":"'"${line}"'"}' \
--url "http://${host}:${port}${REST_URL}"

bash encapsulate command options in variables

I have a bunch of these to test my RESTful API
$CURL \
-v \
-H 'Content-Type: application/json' \
-H 'Accept: application/json' \
-X POST \
-d '{"user":{"email":"user#example.com","password":"secret"}}' \
$URL/$PATH/sessions
I kinda want to shorten it to something like
CURLOPTS="-v -H 'Content-Type: application/json' -H 'Accept: application/json'"
$CURL \
$CURLOPTS \
-X POST \
-d '{"user":{"email":"user#example.com","password":"secret"}}' \
$URL/$PATH/sessions
but the options don't seem to be passed in. Any clues?
Short answer: see BashFAQ #50:'m trying to put a command in a variable, but the complex cases always fail!.
Long answer: Putting commands (or parts of commands) into variables and then getting them back out intact is complicated. The reason your script doesn't work is because of the order in which the shell parses the command line: it parses (and removes) quotes and escapes, then replaces variable values. By the time $CURLOPTS gets replaced, it's too late for the quotes to have their intended effect; instead, they're passed to curl as part of the arguments, which confuses curl greatly.
The solution: store the options in an array rather than a plain string:
CURLOPTS=(-v -H 'Content-Type: application/json' -H 'Accept: application/json')
$CURL \
"${CURLOPTS[#]}" \
-X POST \
-d '{"user":{"email":"user#example.com","password":"secret"}}' \
"$URL/$PATH/sessions"
You can use an array and trigger word splitting
$ set -x
$ CURLOPTS=(-v -H 'Content-Type: application/json' -H 'Accept: application/json')
$ : curl "${CURLOPTS[#]}"
+ : curl -v -H 'Content-Type: application/json' -H 'Accept: application/json'

Resources