Curl request to batch or windows command line [closed] - windows

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 10 months ago.
Improve this question
I would like to create a github repository using a command line/batch script or whatever on windows. This request works on https://reqbin.com/curl, but I can't get it to work on windows.
Thanks
curl -i -H "Authorization: token MY_TOKEN"
-d '{
"name": "test",
"auto_init": true,
"private": false
}'
https://api.github.com/user/repos

curl on Windows is weird and doesn't recognize ' as a valid character, so you need double quotes for everything. Unfortunately, the only way to have double quotes inside of double quotes is to escape the inner quotes:
curl -ki -X POST -H "Accept: application/vnd.github.v3+json" -u "username:TOKEN" -d "{\"name\": \"test\", \"auto_init\": true, \"private\": false}" https://api.github.com/user/repos
Replace username with your actual username and TOKEN with your auth token. You also need -X POST in order to create repos, and the header string that I've added is optional, but strongly recommended.
(Note that while the escape character in batch is ^, the escape character that curl looks for is \. Like I said, curl on Windows is weird.) I've also put everything on one line because multi-line commands have varying degrees of success, while a one-liner always works (as long as it's valid, of course).
See https://docs.github.com/en/rest/repos/repos#create-a-repository-for-the-authenticated-user for more options.

Related

IF command to check if file exists in SharePoint Online using Bash

This question was closed for lack of clarity in a previous post and i was given the option to either edit or re-post the question. I decided to repost the question so others that may had seen it before could see it again but with better clarity. Thanks
i have a folder (e.g. FOLDERX) in a Sharepoint Online Document Library where i use microsoft graph API to GET files by their FILENAME
MS Graph API: https://graph.microsoft.com/v1.0/drives/{drive-id}/root:/filename.txt:/content
Note: these API can only GET files by their filename. The files were the same generic filename each day so i knew what filename to expect hence, i could easily just use the filename in the API call
i run a cron job that GET these file using cURL once a day
so for example, if the file being PUT in the folder each day is "FileA.csv", the API call my in script will look like this which then outputs the file into the remote ubuntu server where the script runs
#!/bin/bash
TOKEN=$(<curl command to retrieve the token needed for the api call>)
#api call that from sharepoint online that outputs to a csv file on remote server
curl -L -X GET "https://graph.microsoft.com/v1.0/drives/<driveID>/root:/FOLDERX/FileA.csv:/content" -H "Authorization: Bearer ${TOKEN}" > FileA.csv
Now, that generic filename isn't generic anymore. the filename is now either "FileA.csv" or "FileB.csv". it's either one each day and i can't keep manually editing the filename on my API call each day.
So i was trying to write an IF statement to first check what filename is available on the Sharepoint Online folder (FOLDERX) each day so when the script is run, the API curl knows which filename to GET using the API curl
Below is where i'm at after trying different IF statement scenarios for over a week now. The API calls are working perfectly. Pls, i just need help writing an IF statement that checks the remote Sharepoint Online folder to see what filename is present and then cURL it using the API call.
#!/bin/bash
TOKEN=$(<curl command to retrieve the token needed for the api call>)
if [ <not sure what to put in here> ];
then
curl -L -X GET "https://graph.microsoft.com/v1.0/drives/<driveID>/root:/FOLDERX/FileA.csv:/content" -H "Authorization: Bearer ${TOKEN}" > FileA.csv
else
curl -L -X GET "https://graph.microsoft.com/v1.0/drives/<driveID>/root:/FOLDERX/FileB.csv:/content" -H "Authorization: Bearer ${TOKEN}" > FileA.csv
fi
EDIT : I do not know how to write the IF statement, that's why i brought it on here.
Just try both, using --fail to make curl let the shell know when it got a 404, 503, or similar.
curl --fail -L -X GET "https://graph.microsoft.com/v1.0/drives/<driveID>/root:/FOLDERX/FileA.csv:/content" -H "Authorization: Bearer ${TOKEN}" >File.csv \
|| curl --fail -L -X GET "https://graph.microsoft.com/v1.0/drives/<driveID>/root:/FOLDERX/FileB.csv:/content" -H "Authorization: Bearer ${TOKEN}" >File.csv \
|| { echo "ERROR: Could not retrieve either FileA or FileB" >&2; exit 1; }

Curl using Environment variables in GitLab-ci.yml

I'm trying to send a slack notification using the .gitlab-ci.yml and I need to pass the Commit's message in the message like this:
"The version ${CI_COMMIT_TAG} Version is available!"
But i'm still not able to get the environment variable desired when receiving the notification on my channel and passing it like this in the file:
script:
- "curl -X POST -H 'Content-type: application/json' --data '{\"text\":\"The version ${CI_COMMIT_TAG} version is available!\"} ' https://hooks.slack.com/services/....../......"
Do you have any clues ? I'm not used to Curl and Yaml
Thanks and Have a good day!
--data '...'
Variable expansion in bash does not work within single quotes. Use double quotes instead.
Alternatively, use a data file to avoid formatting JSON inline:
curl -d "#data.json" ...

How do I format a variable inside the broken double quotes of a curl command?

I am trying to test the Sumo Logic API by updating the information of my collector. The second curl command is the one that is causing the issue 'curl: (55) Failed sending PUT request'. It works in my terminal but not in the bash script.
#!/bin/bash
readonly etag=$(curl -u '<accessId>:<accessKey>' -I -X GET https://api.sumologic.com/api/v1/collectors/<id> | grep -Fi etag | awk '{print $2}' | tr -d \''"\')
echo ${etag}
curl -vvv -u '<accessId>:<accessKey>' -X PUT -H "Content-Type: application/json" -H "If-Match: \"${etag}\"" -T updated_collector.json https://api.sumologic.com/api/v1/collectors/<id>
set -x
The first curl command is assigned to the variable called 'etag' which stores the necessary etag. The etag is used in the second curl command to make a request to update the information stored in the 'updated_collector.json'. The updated_collector.json file is not the issue as I have successfully updated the information via the terminal with it. I suspect the content-type is not being sent in the header because someone ran the script on their end and it was not showing that information with the -vvv tag.
Here you can find the Sumo Logic Collector API Methods and Examples from which I got the curl commands to test the API: https://help.sumologic.com/APIs/Collector-Management-API/Collector-API-Methods-and-Examples
Update: I retieved the etag and then ran the second command in a bash script. I manually inserted the etag into the ${etag} portion of the second curl command. I then ran the script and it worked. Therefore, the etag variable isn't correctly formatted inside the second curl command. I do not know how to fix this.
The issue was partially the syntax but after fixing that, I was still getting an error. "If-Match: \"${etag}\" in my command should be "If-Match: ${etag}" instead. I had to add the --http1.1 flag for it to work. I'm sure this is a sumo logic issue. I am able to execute GET requests no problem using http2.0.

Variable inside double quotes doesn't work as argument [duplicate]

This question already has answers here:
Why does shell ignore quoting characters in arguments passed to it through variables? [duplicate]
(3 answers)
Closed 3 years ago.
I'm trying to pass arguments in a variable to curl and it fails. I turned on set -x to get more information, and I'm really confused about all the single quotes I'm seeing here.
curlopts="-ksS -H 'Content-Type:application/x-www-form-urlencoded'"
$ curl "$curlopts" https://localhost
+ curl '-ksS -H '\''Content-Type:application/x-www-form-urlencoded'\''' https://localhost
curl: option -ksS -H 'Content-Type:application/x-www-form-urlencoded': is unknown
curl: try 'curl --help' or 'curl --manual' for more information
It works without quotes like curl $curlopts https://localhost but I thought it's generally bad practice to use a variable without quotes.
This:
curlopts="-ksS -H 'Content-Type:application/x-www-form-urlencoded'"
sets curlopts to -ksS -H 'Content-Type:application/x-www-form-urlencoded'.
After the above, this:
curl "$curlopts" https://localhost
is equivalent to this:
curl "-ksS -H 'Content-Type:application/x-www-form-urlencoded'" https://localhost
meaning that it calls curl with two arguments: -ksS -H 'Content-Type:application/x-www-form-urlencoded', and https://localhost.
But of course, you want to call curl with four arguments: -ksS, -H, Content-Type:application/x-www-form-urlencoded, and https://localhost.
To achieve that, I recommend using an array:
curlopts=(-ksS -H Content-Type:application/x-www-form-urlencoded)
curl "${curlopts[#]}" https://localhost
where the "${arrayname[#]}" syntax expands, magically, into a separate double-quoted word for each element of the array.
(Note: I've removed the single-quotes because they aren't needed here — none of the characters in Content-Type:application/x-www-form-urlencoded require quoting — but if they make you feel happier, you can safely re-add them.)

Escaping curl command in Windows

I'm trying to run a curl command from the command line in Windows, but for the life of me I can't figure out how I'm supposed to escape it.
I'm executing this:
C:\WINDOWS\system32>curl --anyauth --user user:password -X POST -d "{\"rest-api\":{\"name\":\"BizSimDebug3\"}}" -H "Content-type: application/xml" http://localhost:8002/v1/rest-apis
And I'm getting this:
<rapi:error xmlns:rapi="http://marklogic.com/rest-api">
<rapi:status-code>400</rapi:status-code>
<rapi:status>Bad Request</rapi:status>
<rapi:message-code>RESTAPI-INVALIDCONTENT</rapi:message-code>
<rapi:message>Your bootstrap payload caused the server to throw an error. Underlying error message: XDMP-DOCROOTTEXT: xdmp:get-request-body() -- Invalid root text "{&quot;rest-api&quot;:{&quot;name&quot;:&quot;BizSimDebug3&quot;}}" at line 1</rapi:message>
</rapi:error>
Is there something else I need to do to escape the inner quotes in the -d flag? Or am I overlooking the real issue entirely?
This works in Windows:
curl -i -X POST -H "Content-Type: application/json" -d "{\"Field1\": 123, \"Field2\": 456 }" "http://localhost:8080"
The XDMP-DOCROOTTEXT error indicates the server is trying to parse the payload as XML and failing.
The Content-Type header is telling the server that you're sending XML, but the payload is JSON.
Try changing the Content-Type header to application/json
Quoting is hell. By "Windows Command Line and your prompt I presume you mean cmd.com ?. That doest quote the same as linux shells.
For this simplistic experiment I recommend going for 2 kinds of quotes to avoid escaping But even then its unlikely to work
curl --anyauth --user user:password -X POST -d "{'rest-api':{'name':'BizSimDebug3'}}" -H "Content-type: application/xml" http://localhost:8002/v1/rest-apis
Better luck might be had by going with a unix-like shell such as running cygwin (http://www.cygwin.com/) or maybe xmlsh (www.xmlsh.org) which escape like linux does.
You really are going to have a nightmare running anything complex through the windows command line natively.
-David

Resources