Open Source API Gateway

500 Internal Server Error: Could Not Transform XML to JSON

Membrane API Gateway returns 500 Internal Server Error when the xml2Json plugin cannot convert the XML response of the backend into JSON. The backend labeled its response as XML, but the body cannot be parsed.

The Error Response

The response contains the status 500 Internal Server Error, a detail that starts with Could not transform XML to JSON: followed by the parser's message, the problem type https://membrane-api.io/problems/internal and a link to this page. In development mode, the charset-from-header field shows the charset from the Content-Type header of the backend response. Because the error occurs in the response flow, the client gets a 500. In production mode, the title is replaced by Internal Server Error and the details are left out.

What the xml2Json Plugin Does

The xml2Json plugin converts message bodies with an XML Content-Type into JSON, for example to offer a JSON interface for an XML backend. Messages that are not labeled as XML are passed on unchanged. The same error in the request flow is described under Could not transform XML to JSON in the request.

Common Causes

The Backend Response Is Not Well-Formed XML

The response is labeled as XML but its body is not well-formed XML. A typical case is an HTML error page or an empty body with an XML content type.

The XML Declaration Names an Unsupported Encoding

The XML declaration names an encoding that Java does not support, for example <?xml version="1.0" encoding="foo"?>. The detail then starts with Unsupported encoding:.

The Body Cannot Be Read

The response body cannot be read or decoded, because the Content-Encoding is wrong or the backend aborted the transfer.

How to Fix It

  1. Make the backend return well-formed XML with a supported encoding.
  2. Make the backend label responses that are not XML, such as error pages, with the correct content type.
  3. Check that the charset in the Content-Type header matches the actual encoding of the body.

Hide Details in Production

By default, Membrane runs in development mode and adds details to error responses that help you find the problem. These details may reveal information about your setup. In production, turn them off:

configuration:
production: true
configuration:
  production: true

Add the configuration document to your apis.yaml and separate it from the APIs with ---. The title then reads Internal Server Error, and the details and the link to this page are left out. If the response contains a log key, search the gateway log for it to find the full error message.

Related Documentation

Configuration reference:

Tutorials with runnable examples:

← All Membrane error messages

Still stuck? See the troubleshooting guide or ask on GitHub Issues. For commercial support, contact info@predic8.de.