bash: dynamic string for curl - bash

I'm trying to get files from specific folder, load them with curl and get result to variable:
#!/bin/bash
shopt -s nullglob
FILES=*
for f in $FILES
do
core=$f
#echo "curl -H \"Content-Type: application/json\" --data-binary #$f http://v-cdh-master:8983/solr/$core/update/json?commit=true"
result=$(curl -H "Content-Type: application/json" -s --data-binary #$f http://v-cdh-master:8983/solr/$core/update/json?commit=true)
done
The result of running this script is
curl: no URL specified!
curl: try 'curl --help' or 'curl --manual' for more information
If I remove comment from echo string I could see the valid url like this
curl -H "Content-Type: application/json" --data-binary #affiliates_multival http://v-cdh-master:8983/solr/affiliates_multival/update/json?commit=true
Which works perfect when I copy/paste it to terminal.
Where is my mistake?

The ? in the URL causes the entire argument to be treated as a glob, which is not matching a file. Since you have the nullglob option enabled, the word is removed from the call to curl. Quoting the argument, as you do with the echo command, will resolved the problem by preventing pathname generation.
result=$(curl -H "Content-Type: application/json" -s --data-binary #"$f" "http://v-cdh-master:8983/solr/$core/update/json?commit=true")

Related

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

How to get curl response code in a variable along with output as a file, multiple outputs needs to be appended

I am trying to get the response code of the curl command to a variable and output of the same curl command in a file. Another curl command within a different function will run and the output should append to the same output file.
response=$(curl -i -H "content-type: application/json" -w "%{http_code}" -u "$id:$cred" -H "Accept: application/json" -X POST http://sitename -d '{'<input>'}' >> out.txt
This is giving me neither the response nor the curl output in the file.
Below is working and gives me response as the desired http code though.
response=$(curl -i -H "content-type: application/json" -w "{http_code}" -u "$id:$cred" -H "Accept: application/json" -X POST http://sitename -d '{'<input>'}'
Echo $response

Unable to use variable in curl command [duplicate]

This question already has answers here:
Expansion of variables inside single quotes in a command in Bash
(8 answers)
Closed 3 years ago.
I'm trying to use two variables in a curl command on my ksh program, but it doesn't work.
Example:
Original URL
curl -s --header "Content-Type:application/json" --header "Accept:application/json" -X POST --data-binary '{"username":"foo","password":"foo_pwd"}' URL site
On my program
user=foo
pwd=foo_pwd
curl -s --header "Content-Type:application/json" --header "Accept:application/json" -X POST --data-binary '{"username":"'"$user"'","password":"'"$pwd"'"}' URL site
I've tried also to escape double quote with backslash but it also doesn't work.
First examine your script
user=foo
pwd=foo_pwd
echo '{"username":"'"$user"'","password":"'"$pwd"'"}'
Then run
user=foo
pwd=foo_pwd
curl -s --header "Content-Type:application/json" --header "Accept:application/json" -X POST https://example.com --data-binary '{"username":"'"$user"'","password":"'"$pwd"'"}'

Discard modification content of variable in curl command

I want to create a script to test some web service.
Here the base of my script:
XAuthToken=$(curl -i -s -X POST -H "Content-Type: application/json" -d '{"login": "apitest","password": "apitest"}' http://url:8080/rest-api-web/api/admin | grep -i '^X-Auth-Token:' | sed 's/^[Xx]-Auth-Token: //g')
curl -X GET --header 'Accept: application/json' http://url:8080/rest-api-web/api/admin/delivery_companies?token=$XAuthToken
Result of the first command is:
eyJjdHkiOiJKV1QiLCJlbmMiOiJBMTI4R0NNIiwiYWxnIjoiUlNBLU9BRVAifQ.KmhGpDsor4I7VVPfN1gENzcQb8Yll-wewTRorFu6qfUeaIjKNIAm5lkTkPIpuuOuSpT2U4RgXz9NhwLrYIheW45Du6LnjsyUEjjWGKB4jIW0CMO-79f_-O80pQMq0P09uwmZaqUOmARPjs8UvQYQcuCHhcHd2dB-4WMnfUl-J6USI2LdI-CeOtWDDMw5YEKHGrgnHRzxgUePNWVs-Vc-BR-tXnpkEjIfKmrYy19QUfnwxcWLEWgKisrJw5d7sz7ihfzSueyrh188GiGHTeqqiNiSIkIH8UEyZfW-5BH1Y856IkXlyUaC3abHBRYHuz-O30KwvCKhtCKkaTPVR-uqNw.89jCGtawkkWuyRtq.pQmYHaWZN-XOlbvUthjK76ewqIGd1TBF0Pt7EbA2bDnxsTsXQXyot495-u4Rsvm8-y-rscAZUZFacebMvcvZ-LhUH-jHZH6L5PALY7uvGj_SGApg_Hu8D-adNDlI1rVxNcnHjpOLuOeBdUzKAznbOytquzQNODsjP877IbLTDkWTTj0IC0OWfDu-e4rYxpf2jzZBuEbnRcC8DuvbplNB2tnbRxMjpqnKjicLY9DpYdV1T-tKUt5-sI0Bhlk6tUJ0e28g1McQReUT-sB8pO3vQreEAEsVQlysVq9rC6LiW6RNRHAZP7d6PiuhLS_D_DKVmi85junbMVimYqbvszaJ.1b7PKcZfWmVmNBbTg54nFA
But the second curl in the script, this error occurs: curl: (3) Illegal characters found in URL
And after a check, the content of "XAuthToken" is changed.
Can you help me to escape the character modification?
Thanks you for your comment.
As you can see in the following output, both declare are egal. But the oupt of the command with set -x activate, the Authtoken is changed.
Here the result :
First delare -p :
eyJjdHkiOiJKV1QiLCJlbmMiOiJBMTI4R0NNIiwiYWxnIjoiUlNBLU9BRVAifQ.BTri-oCE-DHYx7UZo8Kib_RoO5bx9lv7BRhEYv2yizyX2n8cwzWWhMPPA-0rDHumaESgc0LmT71NLIJEyZkwN-pXH2MgYdo2vTQhnVdFOL7rZq1rHSJvUxP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HsM6DM8kbkGRHhwFhQSvjq9HUQVNmmKCI.Q4rq7-uzPW3Yewc8Wyl01A

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

Resources