I have a parameter which is container and i want to use this parameter during run curl command. I use like below but it gives an error. Any idea about that? I use it in bash script.
curl -X 'GET' 'https://mycontainer/api/v2.0/projects/testproject/repositories/$(container)/artifacts?page=1&page_size=1&with_tag=true&with_label=false&with_scan_overview=false&with_signature=false&with_immutable_status=false&with_accessory=false' -H 'accept: application/json' -H 'X-Accept-Vulnerabilities: application/vnd.security.vulnerability.report; version=1.1, application/vnd.scanner.adapter.vuln.report.harbor+json; version=1.0' -H 'authorization: Basic YWhtZXQuY2Fua2F5YUBucy5ubDo2MkVEbDIxUEM=' | jq '.[].tags[].name' > output2.txt
use " instead of '.
and for the variable substitution use ${}.
The command would be:
curl -X 'GET' "https://mycontainer/api/v2.0/projects/testproject/repositories/${container}/artifacts?page=1&page_size=1&with_tag=true&with_label=false&with_scan_overview=false&with_signature=false&with_immutable_status=false&with_accessory=false" -H 'accept: application/json' -H 'X-Accept-Vulnerabilities: application/vnd.security.vulnerability.report; version=1.1, application/vnd.scanner.adapter.vuln.report.harbor+json; version=1.0' -H 'authorization: Basic YWhtZXQuY2Fua2F5YUBucy5ubDo2MkVEbDIxUEM=' | jq '.[].tags[].name' > output2.txt
Related
This question already has answers here:
Escaping characters in bash (for JSON)
(13 answers)
Parsing JSON with Unix tools
(45 answers)
Closed 10 days ago.
I have the following GET CURL from which I get an xml.
curl -X 'GET' \
'http://local/something/something2' \
-H 'accept: application/json' \
-H 'authorization: auth'
Now I want to use the previous xml received above within this POST CURL:
curl -X 'POST' \
'http://something/something2' \
-H 'accept: application/json' \
-H 'authorization: auth' \
-H 'Content-Type: application/json' \
-d '{
"components": [
{
"locator": "sample",
"config": xml file from above
}
]
}'
How can I make the second CURL with POST?
See this post to see how to capture the output of the first command into a variable. Use it like this:
output=$(curl -X 'GET' \
'http://local/something/something2' \
-H 'accept: application/json' \
-H 'authorization: auth')
# Assuming the $output variable is a JSON object, with a property
# called 'result', use 'jq' to extract the value of that property
result=$(jq -r '.result' <<< "$output")
# As noted above, escape the double quotes with backslashes
curl -X 'POST' \
'http://something/something2' \
-H 'accept: application/json' \
-H 'authorization: auth' \
-H 'Content-Type: application/json' \
-d "{
\"components\": [
{
\"locator\": \"sample\",
\"config\": \"$result\"
}
]
}"
Note the double quotes - double quotes must be there so $output variable can be used. As a result, the double quotes in the JSON need to be escaped.
I have a script that goes like this where "get_customers" is my pre-defined function and I have to pass each of the following four values as parameter to PUT call for all the customers. However, I am getting the error HTTP 400 Bad Request)","error":"ERROR_BAD_REQUEST" when running this. Anyone knows how I can pass JSON body using this for-loop in PUT call? Is my script wrong?
name=($(get_customers | jq --raw-output '.values[].name'))
tenantId=($(get_customers | jq --raw-output '.values[].tenantId'))
nodeId=($(get_customers | jq --raw-output '.values[].nodeId'))
d=($(get_customers | jq --raw-output '.values[].id'))
for (( i=0; i<${#name[#]}; i++ )); do
curl -X PUT --header "Content-Type: application/json" --header "Accept: application/json" --header "Authorization: Bearer ${API_TOKEN}" -d '{"id":"${d[i]}","name":"${name[i]}","tenantId":"${tenantId[i]}","nodeId":"${nodeId[i]}"}' -k "${URL}/api/file/files/${d[i]}"
done
You are using single quotes, where variables are not expanded. Try this:
for (( i=0; i<${#name[#]}; i++ )); do
curl -X PUT --header "Content-Type: application/json"\
--header "Accept: application/json"\
--header "Authorization: Bearer ${API_TOKEN}"\
-d "$(cat << EOF
{"id":"${d[i]}","name":"${name[i]}","tenantId":"${tenantId[i]}","nodeId":"${nodeId[i]}"}
EOF
)" -k "${URL}/api/file/files/${d[i]}"
done
I am trying to pass a Bearer token (variable $token)to invoke a job via curl command. However the single quote after the -H is not letting the value of variable $token being passed to curl command.
curl -X POST 'https://server.domain.com/v2/jobs/28723316-9373-44ba-9229-7c796f21b099/runs?project_id=aff59748-260a-476e-9578-b4f4a93e7a92' -H 'Content-Type: application/json' -H 'Authorization: Bearer $token -d { "job_run": {} }'
I get this error:
{"code":400,"error":"Bad Request","reason":"Bearer token format is invalid. Expected: 'Bearer '. Received: 'Bearer $token -d { "job_run": {} }'.","message":"Bearer token is invalid."}
I tried adding like the escape character with the variable $token:
curl -X POST 'https://server.domain.com/v2/jobs/28723316-9373-44ba-9229-7c796f21b099/runs?project_id=aff59748-260a-476e-9578-b4f4a93e7a92' -H 'Content-Type: application/json' -H 'Authorization: Bearer "\$token\" -d { "job_run": {} }'
I get the same error:
{"code":400,"error":"Bad Request","reason":"Bearer token format is invalid. Expected: 'Bearer '. Received: 'Bearer "\$token\" -d { "job_run": {} }'.","message":"Bearer token is invalid."}
I tried double quotes as well, it has been a few hours and I am unable to extract he variable value $token within single quotes.
Could some please assist and give me the correct syntax?
Thanks in advance
The problem here are the quotes. It should be like this:
curl -X POST 'https://server.domain.com/v2/jobs/28723316-9373-44ba-9229-7c796f21b099/runs?project_id=aff59748-260a-476e-9578-b4f4a93e7a92' -H 'Content-Type: application/json' -H "Authorization: Bearer $token" -d '{ "job_run": {} }'
In multiline:
curl -X POST 'https://server.domain.com/v2/jobs/28723316-9373-44ba-9229-7c796f21b099/runs?project_id=aff59748-260a-476e-9578-b4f4a93e7a92' \
-H 'Content-Type: application/json' \
-H "Authorization: Bearer $token" \
-d '{ "job_run": {} }'
Specifically, variables in bash aren't interpolated when in single quotes ('). Thus, we set the dynamic string inside double quotes (")
-H "Authorization: Bearer $token"
Also the -H and -d arguments are distinct, they should be quoted separately, in your code you have them combined in a single argument.
this works for me
token=$(curl 'https://example.com/v1.2/auths/login' \
-H 'Accept: application/json, text/plain, */*' \
-H 'Content-Type: application/json' \
--data-raw '{"username":"username","password":"password"}' | jq '.token')
echo $token
#to remove the double quotes from the token string
token=`sed -e 's/^"//' -e 's/"$//' <<<"$token"`
curl 'https://example.com/otherapi' \
-H 'Accept: application/json, text/plain, */*' \
-H "Authorization: Bearer $token"
This question already has answers here:
Difference between single and double quotes in Bash
(7 answers)
Closed 4 years ago.
I am curling Azure Log Analytics for some info, but first I need to grab an OAuth token from 1 command and pass it into the next. I have the following Curl commands which I have tested fine on their own (copying pasting the output for the next input), however I want to pass the OAuth token output as a variable for an automation task, but for some reason it is not able to read the variable into the next command.
token=$(curl -X POST \
https://login.microsoftonline.com/{{subscriptionID}}/oauth2/token \
-H 'Cache-Control: no-cache' \
-H 'Content-Type: application/x-www-form-urlencoded' \
-d 'grant_type=client_credentials&client_id={{clientID}}&client_secret={{clientSECRET}}&resource=https%3A%2F%2Fapi.loganalytics.io' \
| jq .access_token)
curl -X POST \
https://api.loganalytics.io/v1/workspaces/{{workspaceID}}/query \
-H 'Authorization: Bearer $token' \
-H 'Cache-Control: no-cache' \
-H 'Content-Type: application/json' \
-d '{ "query": "AzureActivity | summarize count() by Category" }'
Unfortunately when I run this command it responds back that a token is needed.
{"error":{"message":"Valid authentication was not provided","code":"AuthorizationRequiredError"}}
However, if I were to echo the $token variable it shows that it was saved
beefcake#ubuntu:~$ echo $token
"eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1...."
As I said, the commands work fine if I remove the token=$(..) and just copy/paste the output into the next input. Any ideas why this won't work for automation?
#Aserre had the right mindset. Turns out that jq copies the inverted commas " " from the string, whereas the bearer token requires none. Thus my first command should have looked like this:
token=$(curl -X POST \
https://login.microsoftonline.com/{{subscriptionID}}/oauth2/token \
-H 'Cache-Control: no-cache' \
-H 'Content-Type: application/x-www-form-urlencoded' \
-d 'grant_type=client_credentials&client_id={{clientID}}&client_secret={{clientSECRET}}&resource=https%3A%2F%2Fapi.loganalytics.io' \
| jq -r .access_token)
Note the last line that has the -r command for removing the double quotes. Which showed an echo of:
beefcake#ubuntu:~$ echo $token
eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIs....
Note the " " removed from the echo. In addition to that, I had to alter the next command where I replaced 'Authorization: Bearer $token' with "Authorization: Bearer $token":
curl -X POST \
https://api.loganalytics.io/v1/workspaces/{{workspaceID}}/query \
-H "Authorization: Bearer $token" \
-H 'Cache-Control: no-cache' \
-H 'Content-Type: application/json' \
-d '{ "query": "AzureActivity | summarize count() by Category" }'
I'm trying to run a curl command that puts the value of the date from my bash shell in the string but it doesn't work:
curl -X POST --header "Content-Type: application/json" --header "Accept: application/json" -d "{
\"version\": date +%s
}" "https://api.example.com/"
What am I doing wrong?
date +%s is considered as a regular string in your current command.
Use command substitution to get the needed timestamp value:
curl -X POST -H "Content-Type: application/json" -H "Accept: application/json" \
-d '{"version":'$(date +%s)'}'