POST XML file using cURL command line

XmlCommand LineCurl

Xml Problem Overview


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?

Xml Solutions


Solution 1 - Xml

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.

Solution 2 - Xml

From the [manpage][1], 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

[1]: http://curl.haxx.se/docs/manpage.html "btw, this is the second Google result on 'curl manual'"

Solution 3 - Xml

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

Solution 4 - Xml

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.

Solution 5 - Xml

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"

Solution 6 - Xml

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 

Solution 7 - Xml

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

Solution 8 - Xml

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

Solution 9 - Xml

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionArnab Sen GuptaView Question on Stackoverflow
Solution 1 - XmlPeter SchuetzeView Answer on Stackoverflow
Solution 2 - XmlPiskvor left the buildingView Answer on Stackoverflow
Solution 3 - XmlNgaNguyenDuyView Answer on Stackoverflow
Solution 4 - XmlSteve HHHView Answer on Stackoverflow
Solution 5 - XmlGleb EsmanView Answer on Stackoverflow
Solution 6 - XmlMusleh UddinView Answer on Stackoverflow
Solution 7 - XmlDheeraj RView Answer on Stackoverflow
Solution 8 - XmlPaul VargasView Answer on Stackoverflow
Solution 9 - Xmlalf-manView Answer on Stackoverflow