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. The PHP code was automatically generated for the Curl POST XML example.
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
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
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.