Open Source API Gateway

400 Bad Request: JSON Validation Failed

Membrane API Gateway rejects a message with the title JSON validation failed when the validator plugin finds that the JSON body does not validate against the JSON Schema. If the request is invalid, the client is to blame. If the backend response is invalid, the backend is to blame.

The Error Response

The response contains the title JSON validation failed, a errors field that lists the errors, a flow field that names the direction, the problem type https://membrane-api.io/problems/user/validation and a link to this page. The type user/validation means that a message did not pass validation. The status depends on the direction:

FlowStatusTo Blame
Request400 Bad RequestThe client
Response500 Internal Server ErrorThe backend

In production mode, a 500 has the title Internal Server Error and the shortened type https://membrane-api.io/problems/user.

How the validator Plugin Works

The validator checks the body of a message against a schema. Placed in the request flow, it validates requests. Placed in the response flow, it validates backend responses. With jsonSchema, it validates JSON against a JSON Schema:

api:
port: 2000
flow:
- request:
- validator:
jsonSchema: schemas/order.json
target:
url: https://api.predic8.de
api:
  port: 2000
  flow:
    - request:
        - validator:
            jsonSchema: schemas/order.json
  target:
    url: https://api.predic8.de

The kubernetesValidation plugin uses the same validator and produces the same error. For XML, see XML message validation failed.

Common Causes

The Body Violates the Schema

A required property is missing, a property has the wrong type, a value is out of range or does not match a pattern, or the body contains properties the schema does not allow. The errors field lists each violated constraint.

The Body Is Not JSON

The body is not parseable JSON, for example because of a syntax error, a truncated body or a non-JSON body. The errors field then contains the parser message, for example Could not parse JSON body: ....

The Backend Returns an Invalid Response

With the validator in the response flow, the backend returns a body that does not match the schema, for example after a change in the backend that was not reflected in the schema.

How to Fix It

  1. Read the errors field of the response. It names the violated constraints.
  2. For request errors (400), correct the message in the client.
  3. For response errors (500), fix the backend, or correct the schema if the response is right.

To log validation failures in addition to rejecting them, set failureHandler: log on the validator.

Hide Details in Production

By default, Membrane runs in development mode and adds details to error responses that help you find the problem. In production mode, the link to this page is left out:

configuration:
production: true
configuration:
  production: true

Add the configuration document to your apis.yaml and separate it from the APIs with ---. The list of errors in the errors field is still sent in production mode, because the schema is considered public. To reject messages without listing the errors, set validationDetails: false:

api:
port: 2000
flow:
- request:
- validator:
jsonSchema: schemas/order.json
validationDetails: false
target:
url: https://api.predic8.de
api:
  port: 2000
  flow:
    - request:
        - validator:
            jsonSchema: schemas/order.json
            validationDetails: false
  target:
    url: https://api.predic8.de

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.