Content-Type for XML API Requests
Set the Content-Type header for sending XML data to APIs. Covers application/xml vs text/xml and charset best practices.
Detailed Explanation
XML Content-Type
When sending XML data to an API, the recommended Content-Type is application/xml with an explicit charset declaration.
The Header Value
Content-Type: application/xml; charset=utf-8
application/xml vs text/xml
| Feature | application/xml | text/xml |
|---|---|---|
| Default charset | UTF-8 | US-ASCII |
| Binary safe | Yes | No (text subtype) |
| Recommended by | RFC 7303 | RFC 7303 (with caveats) |
Key difference: When no charset is specified, text/xml defaults to US-ASCII, while application/xml defaults to the encoding declared in the XML prolog (<?xml encoding="..."?>) or UTF-8 if not declared. This makes application/xml safer for international content.
XML Prolog and Charset
The XML prolog can also declare encoding:
<?xml version="1.0" encoding="utf-8"?>
When both the Content-Type charset and the XML prolog encoding are present, the Content-Type charset takes precedence for text/xml, while the prolog takes precedence for application/xml.
Other XML-Based Types
| Media Type | Use Case |
|---|---|
application/xhtml+xml |
XHTML documents |
application/rss+xml |
RSS feeds |
application/atom+xml |
Atom feeds |
image/svg+xml |
SVG images |
application/mathml+xml |
MathML |
curl Example
curl -X POST \
-H "Content-Type: application/xml; charset=utf-8" \
-d '<?xml version="1.0"?><request><action>query</action></request>' \
https://api.example.com/xml
Use Case
Use this when sending XML payloads to REST APIs, RSS/Atom feed processors, or any endpoint that expects structured XML data. Prefer application/xml over text/xml for new implementations.