Skip to content

Api Gateway body validation [Unknown error parsing request body]

0

Hello, We have an Api Gateway REST Api deployed using SAM and we use the body validation with models that worked fine until we encountered the case where a customer write with specials characters. The resource is a PUT /v1/{id}/label with a Lambda integration and a custom authorizer (that succeed). The body validation use the following model:

{
  "required" : [ "label" ],
  "type" : "object",
  "properties" : {
    "label" : {
      "maxLength" : 50,
      "minLength" : 1,
      "type" : "string",
      "description" : "The new label of the vehicle."
    },
    "group" : {
      "maxLength" : 50,
      "minLength" : 1,
      "type" : "string",
      "description" : "The new group of the vehicle"
    }
  }
}

But when making the request with special characters like this: {"label":"TOS056","group":"Чарвак"}, the validation fails with the following $context.error.validationErrorString: "[Unknown error parsing request body]"

I have not found anything in Amazon API Gateway important notes about some characters being not allowed in the body.

The request is made with axios that automatically add the following headers:

      Accept: application/json, text/plain, */*
      Content-Type: application/json
      Content-Length: 35

How can I know what the unknown parsing error is, or is there a list of allowed characters that I have not seen ?

1 Answer
2
Accepted Answer

Verify that the request body is correctly encoded as UTF-8. JSON is typically UTF-8 encoded by default, but you should ensure that your client (Axios in this case) is correctly encoding the payload.

Charset Declaration: Explicitly specify charset=utf-8 in the Content-Type header to ensure the server correctly interprets the character encoding.

Enable Detailed CloudWatch Logs:

Go to the API Gateway console. Select your API and then the Stages section. Choose your stage, and under the Logs/Tracing tab, enable detailed CloudWatch logging. Check CloudWatch logs for detailed error messages that might provide more insight into the validation failure.

AWS
EXPERT
answered 2 years ago
EXPERT
reviewed 2 years ago
AWS
EXPERT
reviewed 2 years ago
EXPERT
reviewed 2 years ago
  • Thank for your quick answer. Looking through the logs and fiddling with the source code of axios, i managed to see that the special characters do not encode the same way and the lenght of the body was correcly computed so it was truncated, causing the failure to parse the JSON.

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.