To post XML using cURL, you need to pass XML data to cURL with the -d command line parameter 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.
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 XML data can be viewed by users. 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
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 as a result of 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
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.