How to post PUT request under the Windows using curl? - windows

I need to post XML data via curl.exe under windows using PUT request.
In the curl help I found:
-d/--data <data> HTTP POST data (H)
What should I supply for <data>?

curl sample calls
# with inlining plain data
curl -X PUT -d "payload" http://localhost
# referrring file
curl -X PUT -d #myXmlFile.xml http://localhost
If your windows curl-port does not support it go for cygwin. It is a linux-like environment for windows and also offers "a proper" curl.

In windows, if a double-quoted argument itself contains a double quote character, the double quote must be doubled.
For example, enter 'This is "quoted" payload' as "This is ""quoted"" payload" which is very different than in Unix.
Example:
curl -X PUT -d "This is ""quoted"" payload" http://localhost

in windows you'll need to put the # inside the quotes for the file you're sending:
curl -XPUT --data-binary "#uploadme.txt"
otherwise you'll get weird errors as it tries to use the content of the file as the url:
curl: (6) Couldn't resolve host 'upload'
curl: (6) Couldn't resolve host 'me!'
(uploadme.txt contains "upload me!")

on Windows CMD, curl refers to C:\Windows\System32\curl.exe so you can use
curl -X PUT -d "payload" http://localhost
instead on Windows PowerShell curl refers to Invoke-WebRequest so it is not working with curl syntax. you can use curl.exe on PowerShell to call C:\Windows\System32\curl.exe so it will solve the issue.
curl.exe -X PUT -d "payload" http://localhost

Related

Generating a Session Key in Windows 10

I am a noob at all this and I am trying to run a Linux curl command in Windows 10 to generate session key. The following is the command in Linux.
curl -H "Content-Type: application/json" -d '{"id":1, "jsonrpc":"2.0", "method": "author_rotateKeys"}' http://localhost:9933/
When I run the command at the Windows command prompt I am receiving the following error.
E:\>curl -H "Content-Type: application/json" -d '{"id":1, "jsonrpc":"2.0", "method": "author_rotateKeys"}' http://localhost:9933/
curl: (3) URL using bad/illegal format or missing URL
curl: (3) URL using bad/illegal format or missing URL
curl: (3) unmatched close brace/bracket in URL position 18:
author_rotateKeys}'
^
E:\>
Any idea how would I covert the command to work with Windows 10? I just need to generate a key for a node I need to connect to from a Windows box.
If you don't have WSL2 setup (useful if you ever need other *nix tools) where that command will just work as is, or don't want to use PowerShell where
curl.exe -H "Content-Type: application/json" -d '{"id":1, "jsonrpc":"2.0", "method": "author_rotateKeys"}' http://localhost:9933/
works (notice the curl is replaced with curl.exe, because in PowerShell 5.1 curl is just an alias for Invoke-WebRequest, it's removed in PowerShell 6 and later, but they're not built-in and won't replace PowerShell 5.1 when installed), then on Command Prompt replace single quote with double quote, and for double quote inside them, escape it (replace it with \")
curl -H "Content-Type: application/json" -d "{\"id\":1, \"jsonrpc\":\"2.0\", \"method\": \"author_rotateKeys\"}" http://localhost:9933/

curl in windows read function returned funny value

I am using the following command to upload file to an endpoint
curl -X POST -H "authorization: Basic base64encode" -H "Content-Type:
multipart/form-data" -H "X-Atlassian-Token: nocheck" -F
"file=#c:/Users/User/Desktop/testresults.xml" https://jira.test-
server.ag/rest/api/latest/issue/man-287/attachments
the command under mac works without problems, under windows I become the following error
curl: (26) read function returned funny value
curl version
curl 7.64.0 (x86_64-w64-mingw32) libcurl/7.64.0 OpenSSL/1.1.1a
installed using https://chocolatey.org
I ran into the same problem when I used curl on the Windows Subsystem for Linux on Windows 10. I had to change the path from C:\Temp\File.txt to /mnt/c/Temp/File.txt.
Check if you can access the file in your shell e.g. with dir c:\path\to\file.txt and if it fails then you know that you have to fix the path first.

How to configure Postman so it will generate curl code for Windows

Postman generates curl code that is not compatible with Windows console. For example, it will generate following curl code for GET https://google.com?q=test:
curl -X GET \
'http://google.com?q=test' \
-H 'Cache-Control: no-cache'
This code cannot be simply copied and pasted into Windows command console as-is. I have to remove backslashes, carriage returns and replace ' with "
Is it possible to configure Postman so it will generate curl code like below?
curl -X GET "http://google.com?q=test" -H "Cache-Control: no-cache"
I'm using Postman application for Windows-64 version 6.1.4
On the "Generate code snippets" modal, there's a "Settings" option.
In order to not have the backslashes/ generate the snippet as oneliner, you need to disable Generate multiline snippet.
There is no option to choose if single or double quotation marks.

Different body for same Curl request sent from Windows and Ubuntu

I'm trying to send a curl request from both Windows and Ubuntu system to a Rest API. following is the request
curl -k -X POST http://172.16.76.1:8080/test -d 'sample_param={"user_info":{"name":"abc","age":"20"}}'
When I read this from the server side, I get the following two different content data from each OS
Body for curl request from Ubuntu:
sample_param={"user_info":{"name":"abc","age":"20"}}
Body for curl request from Windows:
sample_param={user_info:{name:abc,age:20}}
(Note that double quotations are missing)
As a result I cannot get the json object from the request.
Can someone point out the mistake and give a solution for this.
Thanks in advance
Changing the curl command to following worked
curl -k -X POST http://172.16.76.1:8080/test -d "sample_param={\"user_info\":{\"name\":\"abc\",\"age\":\"20\"}}"

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