Curl method with authentification problem - bash

I have a little problem with my bearer:
I have a config file with my variable bearer like this:
bearer = "*********"
And I have my script test.sh :
source /home/name/test/config
curl -X GET \
-H 'Content-Type: application/json' \
-H 'Accept: application/json' \
-H 'Authorization: Bearer $bearer' \https://.../api/vendors \-o
json_file/vendors.json
And then when I open my file vendors.json. It say token invalid or was expired and normaly my token is good, I'm sure. So Where is my problem ?
Thanks

Related

How to pass a string variable which has space characters to curl command in windows cmd

How to pass a string with whitespaces characters to a variable to curl command in windows cmd?
I have to pass a string variable that has white space characters to curl requests in windows cmd.
Below is the command.
set emailID="xxxx#gmail.com"
set openAPISpec="http://petstore/v2/swagger.json"
set licenseKey="ccc0e4-b000-491f-9d7b-7e59f9768"
set projectName="Online Banking REST API OmPf"
set MyDefault="dXNlcjFAbmV0YmFua2luZy5pbzphZG1pbjEyMyDefault"
set MyRoleAdmin="dXNlcjFAbmV0YmFua2luZy5pbzphZG1pbjEyMyRoleAdmin"
set MyRoleUser="dXNlcjFAbmV0YmFua2luZy5pbzphZG1pbjEyMyRoleUser"
curl -s --location --request POST "https://dev.ethicalcheck.qtech.ai/api/v1/scan" -H "Content-Type: application/json" -d "{ \"openAPISpec\": \"%openAPISpec%\", \"email\": \"%emailID%\", \"licenseKey\": \"%licenseKey%\", \"projectName\": \"%projectName%\", \"headers\":[\"Authorization: Bearer %MyDefault%\", \"Authorization: Bearer %MyRoleAdmin%\", \"Authorization: Bearer %MyRoleUser%\"] }"
When I run the above command in windows cmd below is the response
curl -s --location --request POST "https://dev.ethicalcheck.qtech.ai/api/v1/scan" -H "Content-Type: application/json" -d "{ \"openAPISpec\": \"%openAPISpec%\", \"email\": \"%emailID%\", \"licenseKey\": \"%licenseKey%\", \"projectName\": \"%projectName%\", \"headers\":[\"Authorization: Bearer %MyDefault%\", \"Authorization: Bearer %MyRoleAdmin%\", \"Authorization: Bearer %MyRoleUser%\"] }"
{"timestamp":"2022-10-19T03:03:39.094+00:00","status":400,"error":"Bad Request","path":"/api/v1/scan"}
The above command in windows CMD is working perfectly if the variable doesn't have whitespaces characters.
The same was the error I was getting in Linux bash, so I modified the above command accordingly to the Linux environment and it's working perfectly even for whitespaces characters in the variable.
Below is the working command in Linux if a variable has whitespaces characters in it.
export emailID="xxxx#gmail.com"
export openAPISpec="http://petstore/v2/swagger.json"
export licenseKey="ccc0e4-b000-491f-9d7b-7e59f9768"
export projectName="Online Banking REST API OmPf"
export MyDefault="dXNlcjFAbmV0YmFua2luZy5pbzphZG1pbjEyMyDefault"
export MyRoleAdmin="dXNlcjFAbmV0YmFua2luZy5pbzphZG1pbjEyMyRoleAdmin"
export MyRoleUser="dXNlcjFAbmV0YmFua2luZy5pbzphZG1pbjEyMyRoleUser"
curl -s --location --request POST "https://dev.ethicalcheck.qtech.ai/api/v1/scan" -H "Content-Type: application/json" --data-raw '{ "openAPISpec": "'${openAPISpec}'", "email": "'${emailID}'", "licenseKey": "'${licenseKey}'", "projectName": "'"${projectName}"'", "headers":["Authorization: Bearer '${MyDefault}'","Authorization: Bearer '${MyRoleAdmin}'","Authorization: Bearer '${MyRoleUser}'"] }'
Below is the actual thing that is working in Linux bash, I have added extra double quotations to handle whitespaces characters with the projectName variable, and with the email variable it's normal.
"projectName": "'"${projectName}"'"
"email": "'${emailID}'"
The same modification is not working in windows CMD or currently, I'm not able to figure out
#1 Tried to pass this way
\"projectName\": \"\'\"%projectName%\"\'\"
# Command
curl -s --location --request POST "https://api.ethicalcheck.apisec.ai/api/v1/scan" -H "Content-Type: application/json" -d "{ \"openAPISpec\": \"%openAPISpec%\", \"email\": \"%emailID%\", \"licenseKey\": \"%licenseKey%\", \"projectName\": \"\'\"%projectName%\"\'\", \"headers\":[\"Authorization: Bearer %MyDefault%\", \"Authorization: Bearer %MyRoleAdmin%\", \"Authorization: Bearer %MyRoleUser%\"] }"
# Response
{"timestamp":"2022-10-19T03:03:39.094+00:00","status":400,"error":"Bad Request","path":"/api/v1/scan"}
#2 Tried to pass this way
\"projectName\": \"\"%projectName%\"\"
# Command
curl -s --location --request POST "https://api.ethicalcheck.apisec.ai/api/v1/scan" -H "Content-Type: application/json" -d "{ \"openAPISpec\": \"%openAPISpec%\", \"email\": \"%emailID%\", \"licenseKey\": \"%licenseKey%\", \"projectName\": \"\"%projectName%\"\", \"headers\":[\"Authorization: Bearer %MyDefault%\", \"Authorization: Bearer %MyRoleAdmin%\", \"Authorization: Bearer %MyRoleUser%\"] }"
# Response
{"timestamp":"2022-10-19T03:03:39.094+00:00","status":400,"error":"Bad Request","path":"/api/v1/scan"}
So how do I pass a string with whitespaces characters as a variable to curl command in windows CMD?
This link why-is-no-string-output-with-echo-var-after-using-set-var-text-command-line helped in resolving the issue.
Thanks #Mofi
The below syntax worked for me.
set "emailID=xxxx#gmail.com"
set "openAPISpec=http://petstore/v2/swagger.json"
set "licenseKey=ccc0e4-b000-491f-9d7b-7e59f9768"
set "projectName=Online Banking REST API OmPf"
set "MyDefault=dXNlcjFAbmV0YmFua2luZy5pbzphZG1pbjEyMyDefault"
set "MyRoleAdmin=dXNlcjFAbmV0YmFua2luZy5pbzphZG1pbjEyMyRoleAdmin"
set "MyRoleUser=dXNlcjFAbmV0YmFua2luZy5pbzphZG1pbjEyMyRoleUser"
curl -s --location --request POST "https://dev.ethicalcheck.qtech.ai/api/v1/scan" -H "Content-Type: application/json" -d "{ \"openAPISpec\": \"%openAPISpec%\", \"email\": \"%emailID%\", \"licenseKey\": \"%licenseKey%\", \"projectName\": \"%projectName%\", \"headers\":[\"Authorization: Bearer %MyDefault%\", \"Authorization: Bearer %MyRoleAdmin%\", \"Authorization: Bearer %MyRoleUser%\"] }"

Passing array of values in Sinatra GET in a less verbose way

I know Sinatra is not super concise when it comes to this topic. To pass an array of values to a GET controller through query string I'd have to do:
curl -v -H 'ContentType: application/json' -H 'Accept: application/json' 'http://0.0.0.0:8848/my/test?param1[]=1&param1[]=2&param1[]=3'
isn't there a way to do something like:
curl -v -H 'ContentType: application/json' -H 'Accept: application/json' 'http://0.0.0.0:8848/my/test?param1[]=1,2,3'
without then having to split/manipulate the string to get the different values?
You can use one of the usual gem for sinatra: https://github.com/mattt/sinatra-param.
curl -v \
-H 'ContentType: application/json' \
-H 'Accept: application/json' \
'http://0.0.0.0:8848/my/test?param1=1,2,3'
get '/test' do
param :param1, Array
end

Automate cURL to run through a list of ID's

I am attempting to run through a list of 10 contact ID's every ten minutes with cURL
This is the code I am trying to run
curl --request POST \
--url https://youraccountname.api-us1.com/api/3/contactAutomations \
--header 'Accept: application/json' \
--header 'Content-Type: application/json' \
--header 'api-token: test' \
--data '
{
"contactAutomation": {
"contact": THE VARIABLE,
"automation": 23
}
}'
The list of contact variables could just be line items in a text file.
Is this something a bash script with cron could handle - and it just deleted the 10 IDs it runs?
Or is this something I would need to use python and a database to run?
This is really a one off thing - so a cron with a script would be easiest since it doesn't need to be used more than one time through.
If you have a constant list of IDs:
for id in 123 456 555 777
do
curl --request POST \
--url https://youraccountname.api-us1.com/api/3/contactAutomations \
--header 'Accept: application/json' \
--header 'Content-Type: application/json' \
--header 'api-token: test' \
--data "
{
\"contactAutomation\": {
\"contact\": $id,
\"automation\": 23
}
}"
done

Not able to replace the value of the variable inside expression in bash script

I am trying to run a bash script, where I would like to make POST calls in a for loop as follows:
for depId in "${depIds[#]}"
do
echo "$depId" <--------------------------------- THIS IS PRINTING PROPER VALUE
curl 'https://student.service.com/api/student' \
-H 'Accept: application/json' \
-H 'Content-Type: application/json' \
-H 'Cookie: UISESSION=abcd' \
--data-raw '{"name":"Student Name","description":"Dummy","depId":$depId}' \ <---- HERE I CANNOT GET THE VALUE OF THE VARIABLE
--compressed
echo "$content"
done
As mentioned above, I cannot get the value of the department id in the URL, with the above form, I am getting a Request Malformed exception. I have even tried with ${depId}, but no luck.
Could anyone please help here ?
Try flipping your quotes around the variable.
--data-raw '{"name":"Student Name","description":"Dummy","depId":'"$depId"'}' \

curl: 400 Bad Request. The browser (or proxy) sent a request that this server could not understand

I'm getting "400 Bad Request. The browser (or proxy) sent a request that this server could not understand." for the curl command. Curl command is as below, can someone please help?
token=jfkdjfdikdjydve83hhd54rsfdghsghktwhdh87
payload_file=test_fortify_scan.zip
curl -X POST https://dnsname/api/scan \
-H "authorization: Bearer $token" \
-H 'content-type: application/json' \
-d '{"projVer": "dev", "language": "java",
"payloadFile":"$payload_file", "emailList": "myemail#mail.com"}'
Looks like quotes problem. Look at Using curl POST with variables defined in bash script functions
token="jfkdjfdikdjydve83hhd54rsfdghsghktwhdh87"
payload_file="test_fortify_scan.zip"
curl -X POST https://dnsname/api/scan \
-H "authorization: Bearer $token" \
-H 'content-type: application/json' \
-d '{"projVer": "dev", "language": "java",
"payloadFile":"'"$payload_file"'", "emailList": "myemail#mail.com"}'

Resources