Calling curl with an environment variable within the parameters - bash

I am trying to execute a curl command using an environment variable.
curl -u {user}: -X POST --header 'Content-Type: application/json' -d '{"my-var": " '"$ENV_VAR"' "}' https://url
However, this is not correctly fetching the output of $ENV_VAR, and instead passes in the string $ENV_VAR
The above implementation was taken from this question.
How do I correctly pass the value of the environment variable into the curl command?

Related

Ansible Tower: 'extra_vars' with multiple variables are ignored when running with curl in cmd

Can some one tell how to pass multiple extra_vars variables from the command line which will run the Job Template in Tower?
I've followed Ansible documentaion https://docs.ansible.com/ansible-tower/latest/html/userguide/job_templates.html#passing-extra-variables-to-provisioning-callbacks
curl -f -H 'Content-Type: application/json' -XPOST \
-d '{"host_config_key": "efref3d9-740f-429c-43r2-15t326b76", "extra_vars": "{\"Job_ID\": \"24\"},{\"job_templates\": \"test99\"}"}' \
https://tower-ansible.com:443/api/v2/job_templates/822/callback/ -k
For single variable it's working. Below curl command works with single extra variable
curl -f -H 'Content-Type: application/json' -XPOST -d '{"host_config_key": "efref3d9-740f-429c-43r2-15t323b76", "extra_vars": "{\"job_id\": \"24\"}"}' https://tower-ansible.com:443/api/v2/job_templates/822/callback/ -k
According the documentation about Passing Extra Variables to Provisioning Callbacks you have referenced you may
Use the following JSON format as an example when adding your own extra_vars to be passed
"extra_vars": {"variable1":"value1","variable2":"value2",...}
instead of as in your provided example
"extra_vars": {"variable1":"value1"},{"variable2":"value2"}

Curl in shell script not working with payload as variable

I am new to shell. I am using curl in my Jenkins job.
I am getting the expected output if I hardcode the payload in the curl request
curl -i -X POST $url --header "'Content-Type: application/json'" --data-raw {"deployed": true}
But when I try and store it as a variable, I see I am getting errors, even though the curl output I get as part of the Jenkins pipeline is exactly the same
payload='{"deployed": true}'
curl -i -X POST $url --header "'Content-Type: application/json'" --data-raw $payload
Need help in understanding how are the different

Insert a variable between two "

I am trying to run a curl command in linux
curl -i -X POST -H 'Content-Type: application/json' -d '{"configuredLevel": "$LOGGINGLEVEL"}' ${i//$WORDTOREMOVE}
I wanted to insert the variable LOGGINGLEVEL in the payload but I am getting some error with this request.
When I change "$LOGGINGLEVEL" with some other value eg "INFO" I am not getting any errors
Is the correct method to insert a variable between two "

bash script to convert string value to json and then return json

I am very new to bash scripting, please can someone point me in the right direction on how to accomplish my task?
I have a curl call which returns a string, which I want to convert to json.
My curl statement -
curl --insecure -X POST 'https://url/api/IPAM/GetIP' --header 'Content-Type: application/json' --header -d '{"key1": "value1"}'
This curl statement returns a string ,for example: 10.100.100.100
I want to fetch this string and return the output in json format:
{"IP":"10.100.100.100"}
I don't want to use jquery or python to do this because this entire script will be run by a wrapper that only understands bash.
You can use jq to process your IP string into a JSON string and package it into a JSON object of your choice.
ip="10.100.100.100"
jq --arg ip "$ip" -cn '{"IP":$ip}'
Result:
{"IP":"10.100.100.100"}
Now if working with the result of your example curl POST request:
rawip_string=$(curl --insecure -X POST 'https://url/api/IPAM/GetIP' --header 'Content-Type: application/json' --header -d '{"key1": "value1"}')
jq --arg ip "$rawip_string" -cn '{"IP":$ip}'
One way to not rely on external tools like jq, you can get the output ip attribute that to a variable and concatenate it.
$ return=$(echo 10.100.100.100)
$ echo "{\"IP\":\"${return}\"}"
{"IP":"10.100.100.100"}
Like this
printf '{"IP":"%s"}' "$(curl --insecure -X POST 'https://url/api/IPAM/GetIP' --header 'Content-Type: application/json' --header -d '{"key1": "value1"}')"

How to set command line argument using a variable in bash

I set a command line argument as below:
$TOKENARG='Authorization: Bearer 9042f9a3caacc63419be489aefec02a5eae338c33'
curl -v -X DELETE -H $(echo $TOKENARG) http://localhost:3001/api/v1/articles/3
And desired result is:
curl -v -X DELETE -H 'Authorization: Bearer 9042f9a3caacc63419be489aefec02a5eae338c33' http://localhost:3001/api/v1/articles/3
But when I run the above one it doesn't work as expected and returns the following messages:
curl: (6) Could not resolve host: Bearer
* Rebuilt URL to: 9042f9a3caacc63419be489aefec02a5eae338c33
Could not resolve host: 9042f9a3caacc63419be489aefec02a5eae338c33
How should I pass argument using a variable?
Because you don't quote your command substitution, the string is split up into three words, Authorization:, Bearer and 9042f9a3caacc63419be489aefec02a5eae338c33. To prevent that, you could quote it:
curl -v -X DELETE -H "$(echo $TOKENARG)" http://localhost:3001/api/v1/articles/3
But you don't need echo and the command substitution at all: $(command) has the same output as command, so you can use
curl -v -X DELETE -H "$TOKENARG" http://localhost:3001/api/v1/articles/3
Side note: all uppercase variable names should be used for environment variables only, see for example the POSIX spec (fourth paragraph), so better would be
$tokenarg='Authorization: Bearer 9042f9a3caacc63419be489aefec02a5eae338c33'
curl -v -X DELETE -H "$tokenarg" http://localhost:3001/api/v1/articles/3

Resources