Posting XML with Curl [JavaScript/AJAX Code]

To post XML using Curl, you need to pass XML data to Curl with the -d command line option and specify the data type in the body of the POST request message using the -H Content-Type: application/xml command line parameter. In this Curl POST XML example, we also pass the Accept: application/xml request header to tell the server that the Curl client expects XML from the server. Click Run to execute the Curl POST XML Example online and see the results. The JavaScript/AJAX code was automatically generated for the Curl POST XML example.
Posting XML with Curl [JavaScript/AJAX Code] Run
curl -X POST https://reqbin.com/echo/post/xml
   -H "Content-Type: application/xml"
   -H "Accept: application/xml"
   -d "<Request><Login>my_login</Login><Password>my_password</Password></Request>"
Updated: Viewed: 52063 times
JavaScript/AJAX code for Curl POST XML example

JavaScript/AJAX code for Curl POST XML Example

This JavaScript/AJAX code snippet was generated automatically for the Curl POST XML example.
<< Back to the Curl POST XML example

What is Curl?

Curl is a popular command-line tool that allows you to send requests to the server, upload files, and submit web forms. Curl supports over 25+ protocols, including HTTP, HTTPS, SFTP, FTP, and has built-in support for web forms, SSL, user authentication, and HTTP cookies. Curl works on Linux, Mac, Windows.

What is the correct MIME type for XML data?

There are two registered MIME types for XML data: application/xml and text/xml. Use the application/xml when making requests to API endpoints (XML data is not readable by users) and use text/xml when users can view XML data. However, since the introduction of RFC 7303, they should be considered the same in all respects except for the name.

Curl POST XML Request Content-Type Header
Content-Type: application/xml

Why do I need to specify Content-Type when posting XML with Curl?

If you are posting XML data using Curl, you must explicitly specify the data type in the body of the POST request message. Otherwise, Curl will pass the application/x-www-form-urlencoded content type to the server for your data. Therefore, when you submit XML (or any other type of data), you need to explicitly specify the data type using the -H "Content-Type: application/xml" command-line argument.

Why do I need to pass the Accept header when requesting XML using Curl?

If your Curl client expects XML due to a submitted request, it must also send an Accept: application/xml request header to the server. Without this header, the server may return data in a different format.

Curl POST XML Request Accept Header
Accept: application/xml

The server informs the client that it has returned XML data with the Content-Type: application/xml response header.

Curl POST XML Request Example

Example of sending XML data to the ReqBin echo URL.

Curl POST XML Request Example
curl -X POST https://reqbin.com/echo/post/xml
  -H "Content-Type: application/xml"
  -H "Accept: application/xml"
  -d "<xml>....</xml>"

See also

Generate code snippets for JavaScript/AJAX and other programming languages

Convert your Curl POST XML request to the PHP, JavaScript/AJAX, Node.js, Curl/Bash, Python, Java, C#/.NET code snippets using the JavaScript/AJAX code generator.