POST XML file using cURL command line
Asked Answered
R

9

415

How can I POST an XML file to a local server http://localhost:8080 using cURL from the command line?

What command should I use?

Rabies answered 9/6, 2010 at 15:26 Comment(0)
S
534

If that question is connected to your other Hudson questions use the command they provide. This way with XML from the command line:

$ curl -X POST -d '<run>...</run>' \
http://user:pass@myhost:myport/path/of/url

You need to change it a little bit to read from a file:

 $ curl -X POST -d @myfilename http://user:pass@myhost:myport/path/of/url

Read the manpage. following an abstract for -d Parameter.

-d/--data

(HTTP) Sends the specified data in a POST request to the HTTP server, in the same way that a browser does when a user has filled in an HTML form and presses the submit button. This will cause curl to pass the data to the server using the content-type application/x-www-form-urlencoded. Compare to -F/--form.

-d/--data is the same as --data-ascii. To post data purely binary, you should instead use the --data-binary option. To URL-encode the value of a form field you may use --data-urlencode.

If any of these options is used more than once on the same command line, the data pieces specified will be merged together with a separating &-symbol. Thus, using '-d name=daniel -d skill=lousy' would generate a post chunk that looks like 'name=daniel&skill=lousy'.

If you start the data with the letter @, the rest should be a file name to read the data from, or - if you want curl to read the data from stdin. The contents of the file must already be URL-encoded. Multiple files can also be specified. Posting data from a file named 'foobar' would thus be done with --data @foobar.

Somatist answered 9/6, 2010 at 17:55 Comment(10)
thanks Peter a lot!! you were bang on. it's related to that only and u are absolutely correct..thanks once again. :-)Rabies
Read the manpage. The contents of the file must already be URL-encoded. OP's XML files surely aren't.Sleeve
So long as you specify the content-type --header "Content-Type:application/xml" you aren't expected to URL-encodeSleeve
I'll just add that if you use "-d" it defaults to POST, so there is no need to use "-X" as well.Panic
if you are using curl's follow redirects option -L, do not use -X POST as it will make redirected request use POST too. If you just use -d as @Tai suggests, this won't happenAlthough
Here's a quick and dirty curl command to run from a windows command line. I had trouble with the single quotes; seems to be a linux-windows disparity. Anyway, here you go: curl -X POST -d "<run><log>4142430A</log><result>0</result><duration>2000</duration></run>" localhost:8080/ControllerName/ActionNameCallender
As of curl 7.18.0 there is also a --data-urlencode <data> option which will do the URL-encoding for youStart
The command in this answer didn't work for me. curl -F [email protected] http://127.0.0.1:8080 worked instead.Dinitrobenzene
Be warned that -d strips line breaks from files. To avoid this, use --data-binary instead.Aquiline
@RandallCook Apparently because your server expected to see multipart/form-data, not a binary blob. Both cases have their uses.Tache
S
197

From the manpage, I believe these are the droids you are looking for:

-F/--form <name=content>

(HTTP) This lets curl emulate a filled-in form in which a user has pressed the submit button. This causes curl to POST data using the Content-Type multipart/form-data according to RFC2388. This enables uploading of binary files etc. To force the 'content' part to be a file, prefix the file name with an @ sign.

Example, to send your password file to the server, where 'password' is the name of the form-field to which /etc/passwd will be the input:

curl -F password=@/etc/passwd www.mypasswords.com

So in your case, this would be something like
curl -F file=@/some/file/on/your/local/disk http://localhost:8080

Sabbath answered 9/6, 2010 at 15:48 Comment(7)
This solution has the added benefit of being able to name the file being sent (unlike the chosen solution above), and you can therefore e.g. send multiple files.Canonicate
The format file=@- is helpful if you want to pipe your file in.Knurl
In case anyone is looking to send a file and other information: use -F key=val -F key=val as many times as you need. Enclosing all post parameters in one large string separated by & does not work.Engadine
Can you mention how to prepare the receiving web page to be able to receive the files uploaded via curl from desktop?Nuzzi
@Cupidvogel: Like any other file upload field; there is no special handling specifically for curl.Sabbath
Oh okay, so just like a normal backend web page handles a file upload say through a form, same here as well?Nuzzi
@Cupidvogel: Yes. Essentially, cURL simulates a browser's behavior here.Sabbath
C
53

You can using option --data with file.

Write xml content to a file named is soap_get.xml and using curl command to send request:

curl -X POST --header "Content-Type:text/xml;charset=UTF-8" --data @soap_get.xml your_url

Chalk answered 9/6, 2010 at 15:26 Comment(2)
This should be the answer since the OP is asking about posting a file not inline text.Shieh
This was necessary for it to show up "non escaped" for me using spring web server, thanks!Octodecillion
C
21

With Jenkins 1.494, I was able to send a file to a job parameter on Ubuntu Linux 12.10 using curl with --form parameters:

curl --form name=myfileparam --form file=@/local/path/to/your/file.xml \
  -Fjson='{"parameter": {"name": "myfileparam", "file": "file"}}' \
  -Fsubmit=Build \
  http://user:password@jenkinsserver/job/jobname/build

On the Jenkins server, I configured a job that accepts a single parameter: a file upload parameter named myfileparam.

The first line of that curl call constructs a web form with a parameter named myfileparam (same as in the job); its value will be the contents of a file on the local file system named /local/path/to/your/file.txt. The @ symbol prefix tells curl to send a local file instead of the given filename.

The second line defines a JSON request that matches the form parameters on line one: a file parameter named myfileparam.

The third line activates the form's Build button. The forth line is the job URL with the "/build" suffix.

If this call is successful, curl returns 0. If it is unsuccessful, the error or exception from the service is printed to the console. This answer takes a lot from an old blog post relating to Hudson, which I deconstructed and re-worked for my own needs.

Conservative answered 12/3, 2013 at 21:58 Comment(0)
W
8

Here's how you can POST XML on Windows using curl command line on Windows. Better use batch/.cmd file for that:

curl -i -X POST -H "Content-Type: text/xml" -d             ^
"^<?xml version=\"1.0\" encoding=\"UTF-8\" ?^>                ^
    ^<Transaction^>                                           ^
        ^<SomeParam1^>Some-Param-01^</SomeParam1^>            ^
        ^<Password^>SomePassW0rd^</Password^>                 ^
        ^<Transaction_Type^>00^</Transaction_Type^>           ^
        ^<CardHoldersName^>John Smith^</CardHoldersName^>     ^
        ^<DollarAmount^>9.97^</DollarAmount^>                 ^
        ^<Card_Number^>4111111111111111^</Card_Number^>       ^
        ^<Expiry_Date^>1118^</Expiry_Date^>                   ^
        ^<VerificationStr2^>123^</VerificationStr2^>          ^
        ^<CVD_Presence_Ind^>1^</CVD_Presence_Ind^>            ^
        ^<Reference_No^>Some Reference Text^</Reference_No^>  ^
        ^<Client_Email^>[email protected]^</Client_Email^>       ^
        ^<Client_IP^>123.4.56.7^</Client_IP^>                 ^
        ^<Tax1Amount^>^</Tax1Amount^>                         ^
        ^<Tax2Amount^>^</Tax2Amount^>                         ^
    ^</Transaction^>                                          ^
" "http://localhost:8080"
Walford answered 20/11, 2013 at 16:16 Comment(1)
How do u format the xml string before using with curl commandShelley
S
5

You can use this command:

curl -X POST --header 'Content-Type: multipart/form-data' --header 'Accept: application/json' --header 'Authorization: <<Removed>>' -F file=@"/home/xxx/Desktop/customers.json"  'API_SERVER_URL' -k 
Shaikh answered 31/12, 2017 at 5:43 Comment(0)
O
3

If you are using curl on Windows:

curl -H "Content-Type: application/xml" -d "<?xml version="""1.0""" encoding="""UTF-8""" standalone="""yes"""?><message><sender>Me</sender><content>Hello!</content></message>" http://localhost:8080/webapp/rest/hello
Occident answered 8/8, 2014 at 13:7 Comment(0)
E
3

If you have multiple headers then you might want to use the following:

curl -X POST --header "Content-Type:application/json" --header "X-Auth:AuthKey" --data @hello.json Your_url
Electrosurgery answered 23/7, 2015 at 5:17 Comment(0)
S
-1

Powershell + Curl + Zimbra SOAP API

${my_xml} = @"
<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\">
  <soapenv:Body>
   <GetFolderRequest xmlns=\"urn:zimbraMail\">
    <folder>
       <path>Folder Name</path>
    </folder>
   </GetFolderRequest>
  </soapenv:Body>
</soapenv:Envelope>
"@

${my_curl} = "c:\curl.exe"
${cookie} = "c:\cookie.txt"

${zimbra_soap_url} = "https://zimbra:7071/service/admin/soap"
${curl_getfolder_args} = "-b", "${cookie}",
            "--header", "Content-Type: text/xml;charset=UTF-8",
            "--silent",
            "--data-raw", "${my_xml}",
            "--url", "${zimbra_soap_url}"

[xml]${my_response} = & ${my_curl} ${curl_getfolder_args}
${my_response}.Envelope.Body.GetFolderResponse.folder.id
Syndesmosis answered 8/1, 2021 at 18:5 Comment(1)
The question is not asking to use powershell + curl + zimbra SOAP APIPuccoon

© 2022 - 2024 — McMap. All rights reserved.