curl command syntax for windows - windows

I have the following command which works well on Linux, but not on windows. I am not able to find any doc for curl syntax for windows. I experimented with the quotes .. but still not working. Can anyone help me with this command so that I can use it on Windows ( I have installed curl.exe in c drive)
curl -X POST -H "Content-Type: application/json" -H "X-Cachet-Token: secret" http://somegoodserver/api/v1/incidents -d '{"name":"Test","message":"Test message","status":"1"}'
The error I get is:
"status":400,"title":"Bad Request","detail":"The request cannot be fulfilled due to bad syntax.","meta": {"details":["The name format is invalid.","The status format is invalid.","The message format is invalid."]}}]}

Windows requires a ^ at the end of each line, double-quotes instead of single-quotes (except within json brackets), and within the json data, all the double-quotes must be escaped.
Here is an example of a GET and a POST that work on Win10 and WinXP command prompt:
curl -X GET ^
-H "authorization: Basic [your authentication string, if needed]" ^
-H "cache-control: no-cache" ^
-H "content-type: application/json" ^
-H "x-forte-auth-organization-id: org_333251" ^
"https://sandbox.forte.net/api/v3/organizations/org_333251/locations/loc_191620/customers/"
curl -X POST ^
-H "authorization: Basic [your authentication string, if needed]" ^
-H "cache-control: no-cache" ^
-H "content-type: application/json" ^
-H "x-forte-auth-organization-id: org_333251" ^
-d "{ \"action\": \"sale\", \"authorization_amount\": 16.99, \"billing_address\": { \"first_name\": \"Paul\", \"last_name\": \"Hurst\" }, \"card\": { \"card_type\": \"visa\", \"name_on_card\": \"Forte James\", \"account_number\": \"4111111111111111\", \"expire_month\": \"12\", \"expire_year\": \"2020\", \"card_verification_value\": \"123\" } }" ^
"https://sandbox.forte.net/api/v3/organizations/org_333251/locations/loc_191620/transactions" ^

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

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

Replacing IP in curl command with bash variable

I'm currently trying to make a DDNS script that interacts with the Cloudflare API to catch changes in my ip address and automatically fix the ip address change for my web server. Everything is working correctly so far except I can't get $IP to be put properly in the curl statement. I first run a python script from within the bash script to get the ip address, then run the curl statement in the bash script. Here's what the python script looks like (it returns an ip address like "1.1.1.1" with quotations included because the curl command requires the quotations)
#!/usr/bin/python3
import subprocess as sp
def main():
command = "dig +short myip.opendns.com #resolver1.opendns.com";
ip = sp.check_output(command, shell=True).decode('utf-8').strip('\n');
ip_tmp = ip;
ip_tmp = '"' + ip + '"';
ip = ip_tmp;
print(ip);
if __name__ == "__main__":
main();
And the bash script looks like this:
#!/bin/bash
IP=$("./getIP.py")
curl -X PUT "https://api.cloudflare.com/client/v4/zones/zone_id/dns_records/dns_id" \
-H "X-Auth-Email: example.com" \
-H "X-Auth-Key: authkey" \
-H "Content-Type: application/json" \
--data '{"type":"A","name":"example.com","content":$IP,"ttl":120,"proxied":true}'
I've tried to have the python script only return numbers and then added the quotations in the bash script and now vice versa and I can't seem to get it to work. The last line should end up looking like this once the variable replaces with quotations around the ip address:
'{"type":"A","name":"example.com","content":"127.0.0.1","ttl":120,"proxied":true}'
The single quotes around your json structure prevent the variable from expanding.
You have a few options that are readily available.
Ugly quote escaping inside/around your json.
"{\"type\":\"A\",\"name\":\"example.com\",\"content\":$IP,\"ttl\":120,\"proxied\":true}"
Having the python write this data to a file and telling curl to use that file for the source of the post data.
curl -X PUT "https://api.cloudflare.com/client/v4/zones/zone_id/dns_records/dns_id" \
-H "X-Auth-Email: example.com" \
-H "X-Auth-Key: authkey" \
-H "Content-Type: application/json" \
--data #file_you_wrote_your_json_to.json
Using the python requests or urllib modules to issue the request to cloud flare.
Update your main() function to return the IP instead of print it.
my_ip = main()
url = "https://api.cloudflare.com/client/v4/zones/zone_id/dns_records/dns_id"
myheaders = {
"X-Auth-Email": "example.com",
"X-Auth-Key": "authkey",
"Content-Type": "application/json"
}
myjson = {
"type":"A",
"name":"example.com",
"content":my_ip,
"ttl":120,
"proxied":true
}
requests.put(url, headers=myheaders, data=myjson)
Better yet, just do it in bash. Cloudflare DDNS on github.
One shot to fetch the dynamic A-record ID:
curl -X GET "https://api.cloudflare.com/client/v4/zones/**Zone ID** \
/dns_records?type=A&name=dynamic" \
-H "Host: api.cloudflare.com" \
-H "User-Agent: ddclient/3.9.0" \
-H "Connection: close" \
-H "X-Auth-Email: example#example.com" \
-H "X-Auth-Key: "**Authorization key**" \
-H "Content-Type: application/json"
Cron job (* * * * *) to set the dynamic A-record:
#/usr/bin/env sh
AUTH_EMAIL=example#example.com
AUTH_KEY=** CF Authorization key **
ZONE_ID=** CF Zone ID **
A_RECORD_NAME="dynamic"
A_RECORD_ID=** CF A-record ID from cloudflare-dns-id.sh **
IP_RECORD="/tmp/ip-record"
RECORDED_IP=`cat $IP_RECORD`
PUBLIC_IP=$(curl --silent https://api.ipify.org) || exit 1
if [ "$PUBLIC_IP" = "$RECORDED_IP" ]; then
exit 0
fi
echo $PUBLIC_IP > $IP_RECORD
RECORD=$(cat <<EOF
{ "type": "A",
"name": "$A_RECORD_NAME",
"content": "$PUBLIC_IP",
"ttl": 180,
"proxied": false }
EOF
)
curl "https://api.cloudflare.com/client/v4/zones/$ZONE_ID \
/dns_records/$A_RECORD_ID" \
-X PUT \
-H "Content-Type: application/json" \
-H "X-Auth-Email: $AUTH_EMAIL" \
-H "X-Auth-Key: $AUTH_KEY" \
-d "$RECORD"

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

bash variable in curl command

In below example (which I got it from PagerDuty webpage):
machine="hi"
curl -H "Content-type: application/json" -X POST \
-d "{ \"service_key\": \"e93facc04764012d7bfb002500d5d1a6\", \"description\": \"FAILURE for production/HTTP on machine $machine\" }" \
"https://events.pagerduty.com/generic/2010-04-15/create_event.json"
I want to use variables in description like:
"description": "FAILURE for $machine",
However it does not work and it only shows me the "FAILURE for $machine",
I tried "FAILURE for ${machine}", but it does not work too. Do you know how to solve it?
The problem is that use use single quotes. You need to use double quotes and escape and double quote in the string:
curl -H "Content-type: application/json" -X POST \
-d "{
\"service_key\": \"e93facc04764012d7bfb002500d5d1a6\",
...
\"description\": \"FAILURE for production/HTTP on machine $machine\"
}" \
"https://events.pagerduty.com/generic/2010-04-15/create_event.json"
Quite tedious, but it will do the job.

Resources