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?

2240
  • 1,547
  • 2
  • 12
  • 30
Arnab Sen Gupta
  • 5,639
  • 5
  • 24
  • 17

9 Answers9

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.

rogerdpack
  • 62,887
  • 36
  • 269
  • 388
Peter Schuetze
  • 16,185
  • 4
  • 44
  • 58
  • 1
    thanks Peter a lot!! you were bang on. it's related to that only and u are absolutely correct..thanks once again. :-) – Arnab Sen Gupta Jun 10 '10 at 06:54
  • 3
    Read the manpage. `The contents of the file must already be URL-encoded.` OP's XML files surely aren't. – Colonel Panic May 23 '12 at 14:29
  • 56
    So long as you specify the content-type `--header "Content-Type:application/xml"` you aren't expected to [URL-encode](http://en.wikipedia.org/wiki/Percent-encoding) – Colonel Panic May 23 '12 at 14:48
  • 14
    I'll just add that if you use "-d" it defaults to POST, so there is no need to use "-X" as well. – Tal Liron Sep 04 '13 at 12:10
  • 5
    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 happen – Tristan Havelick Apr 21 '14 at 17:18
  • 1
    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 "4142430A02000" http://localhost:8080/ControllerName/ActionName – justian17 May 21 '14 at 13:55
  • 2
    As of curl 7.18.0 there is also a `--data-urlencode ` option which will do the URL-encoding for you – John Hascall Aug 06 '14 at 14:21
  • 1
    The command in this answer didn't work for me. `curl -F filedata=@myfile.foo http://127.0.0.1:8080` worked instead. – Randall Cook Jan 02 '15 at 18:45
  • 21
    Be warned that `-d` strips line breaks from files. To avoid this, use `--data-binary` instead. – Yar Oct 22 '15 at 10:15
  • @RandallCook Apparently because your server expected to see multipart/form-data, not a binary blob. Both cases have their [uses](https://github.com/json-api/json-api/issues/246#issuecomment-669265388). – x-yuri Apr 07 '21 at 12:39
198

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

Piskvor left the building
  • 91,498
  • 46
  • 177
  • 222
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

NgaNguyenDuy
  • 1,306
  • 17
  • 13
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.

Steve HHH
  • 12,947
  • 6
  • 68
  • 71
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^>john@smith.com^</Client_Email^>       ^
        ^<Client_IP^>123.4.56.7^</Client_IP^>                 ^
        ^<Tax1Amount^>^</Tax1Amount^>                         ^
        ^<Tax2Amount^>^</Tax2Amount^>                         ^
    ^</Transaction^>                                          ^
" "http://localhost:8080"
Gleb Esman
  • 1,221
  • 2
  • 10
  • 7
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 
f.khantsis
  • 3,256
  • 5
  • 50
  • 67
Musleh Uddin
  • 71
  • 1
  • 2
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
whoan
  • 8,143
  • 4
  • 39
  • 48
Dheeraj R
  • 701
  • 9
  • 17
2

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
Paul Vargas
  • 41,222
  • 15
  • 102
  • 148
0

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
alf-man
  • 66
  • 4