How do I get an XML from the server? [Python Code]

To retrieve XML from the server, you need to send a GET request and specify the Accept: application/xml header in the request. The Accept header tells the server that your client is expecting XML data. Without this header, the server may return data in a different format. The Content-Type: application/xml response header informs the client that the server returned XML. In this GET XML Example, we are sending a GET request to the ReqBin echo URL. Click Send to execute the GET XML request online and see the results. The Python code was automatically generated for the GET XML example.
How do I get an XML from the server? [Python Code] Send
GET /echo/get/xml HTTP/1.1
Host: reqbin.com
Accept: application/xml

Updated: Viewed: 14574 times
Python code for GET XML example

Python code for GET XML Example

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

What is XML?

XML (Extensible Markup Language) is an extensible markup language that provides structured information: data, documents, configuration, and more. XML is called extensible because it doesn't fix the markup used in documents: you can create markup according to the needs of a particular area, limited only by the rules of XML syntax. An XML file is a simple text file that uses XML tags to describe the structure of a document, how it is stored and transported, and the data itself.

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

What is HTTP GET request?

HTTP GET is the most popular of the nine commonly used HTTP methods. The GET request method is used to retrieve data from the specified URL, cannot contain data in the body of the GET request, and should not change the server state. The GET method is defined as idempotent, which means that several similar GET requests should have the same effect on the server as a single request.

HTTP GET Request Example
GET /echo/ HTTP/1.1
Host: reqbin.com

Get XML Example

An example of getting to our XML GET request:

Get XML Request Example
GET /echo/get/xml HTTP/1.1
Host: reqbin.com
Accept: application/xml

An example of a server response to our XML GET request:

Server XML Response Example
<?xml version="1.0" encoding="utf-8"?>
<Response>
    <ResponseCode>0</ResponseCode>
    <ResponseMessage>Success</ResponseMessage>
</Response>

Why is it important to send the Accept header in the XML request?

The "Accept: application/xml" header indicates that the client expects to see XML in the server's response. Without this header, the server can return data in a different format.

Accept Header Example
Accept: application/xml

When the server returns XML in its response, it notifies the client using the "Content-Type: application/xml" response header.

Content-Type Response Header Example
Content-Type: application/xml

How to post XML to the server?

Posting XML data to the server requires sending an HTTP POST request, including the XML in the body of the message, and setting the correct MIME type. XML files should be MIME type application/xml. The Accept: application/xml request header tells the server that the client is expecting XML, and the Content-Type: application/xml response header indicates that the server returned XML.

POST XML Request Example
POST /echo/post/xml HTTP/1.1
Host: reqbin.com
Content-Type: application/xml
Accept: application/xml
      
<?xml version="1.0" encoding="utf-8"?>
<User>
    <name>name</name>
    <rating>rating</rating>
</User>

How to view XML directly in the browser?

Most browsers can display XML documents right in the browser. To do this, enter the address of the XML document in the address bar, and the browser will display the XML in a convenient tree format.

See also

Generate code snippets for Python and other programming languages

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