Open Source API Gateway

500 Internal Server Error: Could Not Replace Body

Membrane API Gateway returns 500 Internal Server Error with the detail Could not replace body! when the regExReplacer plugin fails to apply its replacement to the message body. The regex and replace settings are usually to blame, sometimes a body that cannot be read.

The Error Response

The response contains the status 500 Internal Server Error, the title Internal server error., the detail Could not replace body!, the problem type https://membrane-api.io/problems/internal and a link to this page. In development mode, the exception message is included as well. The type internal means that the gateway could not complete the processing of the exchange. The plugin can fail in the request or the response flow. In both cases the client receives 500. In production mode, the title is replaced by Internal Server Error and the details are left out.

What the RegEx Replacer Does

The regExReplacer plugin replaces every match of regex with replace, in the message body or, with target: header, in the header values. It uses Java regular expression syntax, and replace may refer to groups with $1, $2 and so on.

Common Causes

The Regular Expression Is Invalid

regex is not a valid Java regular expression. It is compiled on every request, not at startup, so a syntax error only shows up here.

The Replacement Refers to a Missing Group

replace refers to a group that does not exist, for example $2 with one group, or it contains an unescaped $ or \, which Java treats as group references and escapes.

The Body Cannot Be Read

The client or backend closed the connection in the middle of the body, or the body uses a Content-Encoding that Membrane cannot decode.

An Error with Target Header

With target: header, an invalid regex or replace is not caught by the plugin. It is reported under the same URL by Membrane's generic error handler, with a detail that starts with Aborting! Exception caused in …. The same handler reports any other unexpected exception of the plugin; unless it points to your configuration, this usually indicates a bug in Membrane.

How to Fix It

  1. Test regex and replace with Java regex semantics.
  2. Escape a literal $ as \$ and a literal \ as \\ in replace.
  3. If the body is compressed with an encoding Membrane cannot decode, have the client or backend send it uncompressed.
  4. Look at the exception in the response (development mode) or in the log.

A replacement with one group. Single quotes keep YAML from interpreting the backslashes:

api:
port: 2000
flow:
- regExReplacer:
regex: 'price: (\d+)'
replace: 'price: $1 EUR'
target:
url: https://api.predic8.de
api:
  port: 2000
  flow:
    - regExReplacer:
        regex: 'price: (\d+)'
        replace: 'price: $1 EUR'
  target:
    url: https://api.predic8.de

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 instead of the details the response only contains a log key. Search the gateway log for this key to find the exception.

If the plugin is nested inside a request, response or abort element and throws an unexpected exception, the exception message, the stack trace and the link to this page are currently sent even in production mode. Fix the failing plugin rather than relying on production mode to hide them.

Related Documentation

Configuration reference:

← All Membrane error messages

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