Search This Blog

Sunday, September 13, 2015

What's the difference between text/xml vs application/xml for webservice response?

XML has two MIME types, application/xml and text/xml. These are often used interchangeably, but there is a subtle difference which is why application/xml is generally recommended over the latter.

Let me explain why: according to the standard, text/*-MIME types have a us-ascii character set unless otherwise specified in the HTTP headers. This effectively means that any encoding defined in the XML prolog (e.g. <?xml version=”1.0” encoding=”UTF-8”?>) is ignored. This is of course not the expected and desired behavior.

To further complicate matters, most/all browser implementations actually implement nonstandard behavior for text/xml because they process the encoding as if it were application/xml.

application/xml is generally the preferred mime type. For text/xml it appears the encoding will generally be treated as us ascii regardless of what is specified in the XML document header (unless otherwise specified in the HTTP headers).

From the RFC (3023), under section 3, XML Media Types:

If an XML document -- that is, the unprocessed, source XML document -- is readable by casual users, text/xml is preferable to application/xml. MIME user agents (and web user agents) that do not have explicit support for text/xml will treat it as text/plain, for example, by displaying the XML MIME entity as plain text. Application/xml is preferable when the XML MIME entity is unreadable by casual users.

So, text/* has encoding issues, and is not implemented by browsers in a standards-compliant manner, which is why using application/* is recommended.

1 comment:

Unknown said...

Great explanation! Thanks for all the detail!