Getting XML with Curl [Python Code]

To retrieve an XML from the server using Curl, you need to pass the target URL to Curl along with the -H "Accept: application/xml" command line option. The -H command line switch sends an Accept: application/xml header to the server and tells the server that the Curl client expects an XML response. Without this header, the server may automatically select a different data type for the response and return the data in a different format than XML. In this Curl GET XML Example, we send a request to the ReqBin echo URL with the required HTTP header. Click Run to execute the Curl GET XML request online and see the result. The Python code was automatically generated for the Curl GET XML example.
Getting XML with Curl [Python Code] Run
curl https://reqbin.com/echo/get/xml
   -H "Accept: application/xml" 
Updated: Viewed: 39636 times
Python code for Curl GET XML example

Python code for Curl GET XML Example

This Python code snippet was generated automatically for the Curl GET XML example.
<< Back to the Curl GET XML example

What is Curl?

Curl (stands for Client URL) is a command-line tool that developers and administrators use to transfer data to and from the server. Curl supports all modern protocols, including HTTP, HTTPS, SFTP, and FTP, and works on almost any platform, including Linux, Windows, and macOS. Curl allows you to communicate with the server by specifying the URL and the data you want to send in command-line parameters and is ideal for testing APIs and use in automation scripts.

What is the HTTP GET request?

HTTP GET is one of the nine standard HTTP methods. The HTTP GET method retrieves the content of the resource from the server using the provided URI. GET request should only receive data and cannot send data to the server in the body of a GET message and should not change the server's state.

Curl GET Request Example
curl https://reqbin.com/echo

What is XML?

XML (Extensible Markup Language) is a simple text format that provides structured information: data, documents, configuration, and more. XML is a way of exchanging data over the Internet and corporate networks. An XML file is a plain text file that uses custom XML tags to describe the structure of a document and how it should be stored and transported.

XML Example
<Request>
   <Login>
      login
   </Login>
   <Password>
      password
   </Password>
</Request>

Why is it important to send Accept header with Curl request?

Your Curl client must indicate that it can accept XML by sending the Accept: application/xml header to receive XML from the server. For example, if the target server might return different MIME types from the same API endpoint, and you don't pass that header, the server might automatically choose the MIMI type it thinks best suits your client, and it might not be XML. To send the Accept: application/xml header to the server, use Curl's -H command-line option. If the server returned XML in its response, it notifies the client using the Content-Type: application/xml response header.

Get XML with Curl
curl [URL] -H "Accept: application/xml"

How to post XML to the server using Curl?

To send XML to the server using Curl, you need to pass the XML data to Curl with the -d command line option and specify the data type in the body of the POST message using the -H "Content-Type: application/xml" command-line option. If you omit the Content-Type header, Curl will send the "Content-Type: application/x-www-form-urlencoded" header along with the XML. If your client is expecting XML data in the server's response, you also need to send the "Accept: application/xml" header with your Curl request, as if you were receiving XML from the server.

Post XML with Curl
curl -X POST [URL]
   -H "Content-Type: application/xml"
   -H "Accept: application/xml"
   -d "[xml_data]"

See also

Generate code snippets for Python and other programming languages

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