REST API XML is an API that follows REST principles and uses XML as its data format instead of JSON. The API itself is defined by how it handles resources and HTTP methods; XML is just the envelope that carries the data. Many older enterprise systems and public APIs still speak XML, so knowing how it works helps you integrate with them without guesswork.
What REST Means Before the XML Part
REST stands for Representational State Transfer. An API is RESTful when it exposes resources as URLs and lets you act on them with standard HTTP methods. A GET request reads a resource, a POST creates one, a PUT or PATCH updates one, and a DELETE removes one. The server returns a representation of the resource, and that representation can be JSON, XML, HTML, or plain text.
So the phrase "REST API XML" describes two separate decisions: the architectural style (REST) and the payload format (XML). You can have a REST API that returns XML, JSON, or both. The client asks for the format it wants, usually through the Accept header.
How XML Fits Into a REST Request
When a REST API uses XML, the request and response bodies are XML documents. A typical GET request looks like this in your head: you send a request to a URL such as /api/books/42, and the server replies with an XML document describing book 42. The response might look like this:
<book>
<id>42</id>
<title>The Quiet Hours</title>
<author>A. Writer</author>
<available>true</available>
</book>
To create a resource, you send a POST request with an XML body and a Content-Type header of application/xml. To update it, you send a PUT or PATCH with the changed fields. The server reads the XML, validates it against its schema, and returns a status code such as 201 for created or 200 for updated.
One practical detail: XML is verbose. Every element needs an opening and closing tag, so the same data takes more bytes than JSON. That matters on slow connections or metered mobile data, but for internal systems on a LAN it rarely causes trouble.
Choosing Between XML and JSON for a REST API
Most new REST APIs use JSON because it is lighter and maps directly to common data types in JavaScript, Python, and most other languages. XML still appears where a system predates JSON, where a schema must be strict, or where the organisation already has XML tooling. Here is a quick comparison:
| Factor | XML | JSON |
|---|---|---|
| Readability for humans | Verbose but structured | Compact and familiar |
| Schema support | Strong, with XSD | Looser, often validated separately |
| Browser and language support | Built in but clunky | Native in JavaScript |
| Typical use | Enterprise, SOAP-adjacent, legacy | Web and mobile apps |
A rule of thumb: if you are building a new API for a web or mobile client, choose JSON. If you must integrate with an existing XML service, keep XML and use a parser rather than string manipulation.
Reading and Writing XML in Your Client
Most languages ship with an XML parser, so you rarely need to handle raw strings. In Python, xml.etree.ElementTree parses a response into a tree you can walk. In JavaScript, DOMParser turns a string into a document you can query with querySelector. In Java, DocumentBuilder does the same job.
When you build a request body, generate the XML from your data structure rather than concatenating strings. String concatenation breaks as soon as a value contains an ampersand or a angle bracket. A builder or a template escapes those characters for you.
- Check the API documentation for the expected XML schema or example.
- Set the Content-Type header to
application/xmlwhen sending a body. - Parse the response with a real XML parser, not a regular expression.
- Handle namespaces if the API uses them; a missing namespace is a common cause of empty results.
- Log the raw response when a request fails so you can see the actual error message.
Common Problems and How to Fix Them
Empty response body. Check the Accept header. If you send Accept: application/json to an XML-only endpoint, the server may return nothing or an error. Set Accept: application/xml.
Parse error on a valid-looking document. XML is strict about a single root element and about closing tags. A stray space before the declaration or a missing closing tag will stop the parser. Validate the document against the schema if one is published.
Namespaces ignored. If the response uses a default namespace, your query may need to include it. In many parsers you register the namespace prefix before searching.
Encoding issues. XML declares its encoding in the first line. If the server sends UTF-8 but the declaration says something else, accented characters break. Check the declaration and the actual bytes.
Large documents. A big XML response can exhaust memory if you load it all at once. Use a streaming parser for documents over a few megabytes.
When XML Is the Right Choice
XML earns its place when you need strict validation, when the data is document-like with mixed content, or when you are integrating with a system that already speaks it. It also appears in APIs that expose Atom feeds or that follow older enterprise conventions. For a simple CRUD API serving a modern front end, JSON is the easier path. The two can coexist: many APIs return JSON by default and XML when asked.
If you are unsure which format an endpoint supports, send a request with no Accept header and read the response, or check the documentation for a format parameter. A well-built API states its supported formats plainly.
Does a REST API have to use JSON?
No. REST is about resources and HTTP methods, not the payload format. XML, JSON, HTML, and plain text are all valid representations.
Can I send XML in a POST request?
Yes, as long as the server accepts it. Set the Content-Type header to application/xml and send a well-formed document with a single root element.
Why does my XML request return a 415 error?
A 415 means the server does not support the media type you sent. Check that the Content-Type header matches what the API expects, and confirm the endpoint accepts XML at all.