syntax use variable in curl script json data in gitlab-ci.yml - gitlab

I am running out of ideas to have this to work in my gitlab-ci.yml.
I have a variable called TARGET, and I want to successfully inject its value inside the json payload of my http POST request in form of curl bash command.
job:
stage: deploy
script:
- curl -k --request POST ${URL} --header "Authorization:Basic ${TOKEN}" --header 'Content-Type:application/json' --data "{"extra_vars":{"target":${TARGET}}}"
variables:
TARGET: host1
the pipeline output keeps complaining with error message:
{"detail":"JSON parse error - Expecting property name enclosed in double quotes: line 1 column 2 (char 1)\nPossible cause: trailing comma."}
I also tried escaping some special characters with \ as below, but not working either:
curl -k --request POST ${URL} --header "Authorization:Basic ${TOKEN}" --header 'Content-Type:application/json' --data "\{"extra_vars":\{"target":${TARGET}\}\}"
your helps are very appreciated.

There are two issues here. You are missing double quotes around the value for target inside the JSON since TARGET is a string value.
You are also wrapping your data argument with double quotes hence you should not use them inside the payload without either escaping them or switching to single quotes.
Keeping that in mind the payload may look something like this:
'{"extra_vars":{"target":"'${TARGET}'"}}'
Here's a quick tip on how to test the output using httpbin.org:
TARGET=host1 && curl -v https://httpbin.org/post -H "Content-Type: application/json" -d '{"extra_vars":{"target":"'${TARGET}'"}}'
Which will respond with the sent payload (which is ofcourse escaped as it is also wrapped inside JSON:
"data": "{\"extra_vars\":{\"target\":\"host1\"}}",

Related

Store output of command in variable where the command is already executed/stored in it's own variable - bash script

I have a command that is stored in a variable. I would like to execute that command and then store the response of that command in a variable. I'm having an issue with syntax errors for the responseVar=$("{commandVar}") line as it seems to trigger a "no such file or directory" error instead of actually running the command/storing the output in the response variable.
commandVar="curl --location -g --request POST 'https://172.217.25.35/rest-gateway/rest/api/v1/auth/token' --header 'Content-Type: application/json' --header 'Authorization: Basic U0FNcWE6NTYyMFNhbSE=' --data-raw '{"grant_type": "client_credentials"}'"
responseVar=$("{commandVar}")
echo "Response of command: ${responseVar}"
Does anyone know the correct syntax on how I would do this? I need to eventually parse the output of the command which is why I need to store the response in it's own variable.
You are trying to execute a program named {commandVar}, and this program does not exist in your PATH.
To start with, you better use an array to store your command, each array element holding one command argument:
commandVar=(curl --location -g --request POST 'https://172.217.25.35/rest-gateway/rest/api/v1/auth/token' --header 'Content-Type: application/json' --header 'Authorization: Basic U0FNcWE6NTYyMFNhbSE=' --data-raw '{"grant_type": "client_credentials"}')
With this, you can execute it by
"${commandVar[#]}"
respectively store its standardoutput into a variable like this:
responseVar=$( "${commandVar[#]}" )

curl command with queryparameter for passing urlencode

I have been hitting a curl command with queryparametr in the request and passing a json format in the query parameter , but I am getting error like (" some well formatted json in parameter query.unexpected character encountered parsing error while parsing value .
curl -g --request GET -H "Content-Type:application/json" -H "apitoken:abcd" "https://odat.abc.com/api/data?query=$(echo '$jsonInput')"
jsonInput is the json format file we are passing here.
See https://stackoverflow.com/a/32980082/1395722
curl -G --request GET -H "Content-Type:application/json" -H "apitoken:abcd" "https://odat.abc.com/api/data" --data-urlencode "query=$(echo $jsonInput)"

retrieve value on post api call - terraform

I have been trying to write a code using terraform which must do an API call on POST and it must get back value on post, this return value on post I must be able to use it else where in the code.
Reading a lot I noticed there are two ways to do it
Mastercard Rest API
Local-Exec and Null provider
The first one also has some issues when we try to fetch a value on post, unfortunately the API I am trying to call needs some input values to return an output. If anyone has experience in doing this using the mastercard rest api please let me know how.
My curl statement looks like this -
curl --insecure -X POST 'https://url.fqdn/get_hostname' --header 'Content-Type: application/json' --header 'Authorization: Basic tokenvalue' -d '{"key": "value","key": "value","key": "value"}'
Using local-exec and null provider
resource "null_resource" "get-hostname" {
provisioner "local-exec" {
command = <<EOF
curl --insecure -X POST 'https://url.fqdn/get_hostname' --header 'Content-Type: application/json' --header 'Authorization: Basic tokenvalue' -d '{"key1": "value1","key2": "value2"}'
EOF
}
}
How can I get the output of the command property ?
You can redirect the output of curl to a file, and process that file within another null_resource block with an explicit dependency on the one that writes the file.

curl command in linux

How we can add JSON parameters in a curl command to fetch data ?
PS: I have tried to fetch data from some source using curl command but it requires passing JSON parameters in it. So how I will accomplish it ?
I think mentioning content type as json and passing data will do the things right
curl -H "Content-Type: application/json" -d '{"key1":"value1","key2":"value2"}' http://domain.com/rest/path/here
Above will work for GET Method, for POST method
curl -H "Content-Type: application/json" -X POST -d '{"key1":"value1","key2":"value2"}' http://domain.com/rest/path/here

Variables in cURL

As you all know tokens can be very long strings and become a hassle to copy and past over and over.
How can I store the token string as a variable and call it when I need it in cURL
example
token: "ABCDefG"
I want to be able to call something like:
curl -L --silent --header "Authorization: GoogleLogin auth=${token}"
Inside a bash script, can't you put the token in a variable, like this:
#!/bin/bash
token="ABCDef"
curl -L --silent --header "Authorization: GoogleLogin auth=$token"
Now in the bash script whenever you need to use the token, you just need to use the variable "$token" (remember to enclose the variable in double quotes).
Or you could set an environment variable:
export token=ABCDef
but it's not an elegant solution
You could store the token in an array
tokens=("ABCDeF" "ASDFGh")
Then when you want to call them, use
curl -L --silent --header "Authorization: GoogleLogin auth=${tokens[0]}"
And if you want to add a token you can
tokens+=("qwerty")

Resources