How to send multiple words in a curl post message - bash

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}"

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.

Cannot import Jelastic Manifest with cURL command using two -H parameters

I'm trying to create a Jelastic Manifest with a cURL command inside it. When I import it, it gives me an error but unfortunately I have no access to the console (disabled by the provider).
The command is the following :
curl -X POST <my_url> -H "Content-Type: application/json" -H "Authorization: Bearer <token>" -d "{}"
Some additionnal information :
The URL is correct 100%
The token does not contain any special characters : Only upper/lowercase characters and numbers
The command is run successfully from the command line
If I remove the first -H parameter, I can import my manifest. Same if I remove the second -H parameter and keep the first one
My guess is that, somehow, having two -H is not considered as valid but I don't know why. Any ideas ?
EDIT : A screenshot of the error shown on the platform
The "two -H parameters" wasn't a root cause in your question.
The thing is that the YAML gets the data you sent as a key/value array (dictionary).
In your example it would be:
curl -s -X POST https://test.com -H "Content-Type - as a key,
and
application/json" -H "Authorization: Bearer xx" -d "{\"Key\":\"Value\"}" - as a value.
If you need an array of strings the YAML may be as this
cmd [cp]:
- 'curl -s -X POST https://test.com -H "Content-Type: application/json" -H "Authorization: Bearer xx" -d "{\"Key\":\"Value\"}"'
If you need a multi-line string it should look like this
cmd [cp]: |
curl -s -X POST https://test.com -H "Content-Type: application/json" -H "Authorization: Bearer xx" -d "{\"Key\":\"Value\"}"

use variable between two question mark in a command option in the bash script

here is two samples:
this is a sample that would cause 400 error
curl -i -k -u $account:password -H "Content-Type: application/json" -X PUT -d '{"source-path": "http://${ip}/LTMBlackList_Postbody${filename_extension}","type":"ip"}' https://$ip2$api2
and this is a normal one, it can get a 200 OK response:
curl -i -k -u $account:$password -H "Content-Type: application/json" -X PUT -d '{"source-path": "http://127.0.0.1/LTMBlackList_Postbody-test.log","type":"ip"}' https://$ip2$api2
how could i called the curl command in script with variable?
The $ip is not expanded because it is in single quotes. First close single quotes then do double quotes, expand the variable, close double quotes and conitnue single quoting.
Remember to always quote your variable expansions to disable word splitting
curl -i -k -u "$account:$password" -H "Content-Type: application/json" -X PUT \
-d '{"source-path": "http:/'"$ip"'/LTMBlackList_Postbody'"$filename_extension"'","type":"ip"}' "https://$ip2$api"

Unterminated quoted string in Capistrano curl command

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.

< was unexpected at this time. from curl command line when posting input data as an xml to rest service

when i post a raw string as an input to JSON REST Service call it is executing ex:
curl -d "{\"input1\": \"as\", \"input2\": \"ad\"}" -i -X POST -H "Content-Type:application/json" http://localhost/rtygies/Service1.svc/rest/receivedata1
But when i am posting as an xml as input it is giveng error as below:
curl -d "{\"input1\": \"<xml></xml>\", \"input2\": \"<xml></xml>\"}" -i -X POST -H "Content-Type:application/json" http://localhost/rtygies/Service1.svc/rest/receivedata1
Error: < was unexpected at this time
I am using curl in windows.
can any one say how to post xml as a string input to Rest service in JSON format from curl
Actually, I don't know what is the case,
but I got an error said < is unexpected this time
when I tried the following command (on windows):
curl -u admin:password -XPOST -H 'Content-type: text/xml' -d '<namespace><prefix>newWorkspace</prefix><uri>http://geoserver.org</uri></namespace>' http://localhost:8080/geoserver/rest/namespaces
Then I changed the single quote into double quotes and it worked.
Redirection symbols can be escaped by "
Try using
curl -d "{\"input1\": \""<xml></xml>"\", \"input2\": \""<xml></xml>"\"}" -i -X POST -H "Content-Type:application/json" http://localhost/rtygies/Service1.svc/rest/receivedata1
I had no luck with double-quotes, however escaping < and > with ^ worked for me.
So your curl becomes..
curl -d "{\"input1\": \"^<xml^>^</xml^>\", \"input2\": \"^<xml^>^</xml^>\"}" -i -X POST -H "Content-Type:application/json" http://localhost/rtygies/Service1.svc/rest/receivedata1

Resources