Could not Parse Request body into JSON Error - AWS CLI

avatar
Borislav Hadzhiev

Last updated: Feb 26, 2024
2 min

banner

# Could not Parse Request body into JSON Error - AWS CLI

The "InvalidRequestContentException - Could not Parse Request body into JSON Error" error occurs when invoking a Lambda function with syntactically incorrect JSON payload, or forgetting to set the --cli-binary-format parameter to raw-in-base64-out.

For example, the following command throws the error.

shell
aws lambda invoke --function-name testFunction --cli-binary-format raw-in-base64-out --payload '{"name": "John Doe",}' response.json

could not parse request body

The reason is the dangling comma in the --payload parameter.

Note that if you're on Windows, you should escape the double quotes in the JSON string - '{\"name\": \"John Doe\"}'.

The fastest way to validate and correct your JSON is to use a JSON validator.

If you paste your payload into the form, the validator checks for errors and sometimes directly fixes them.

The --payload parameter expects base64 encoded input by default. Therefore, when passing a raw JSON string, we have to set the --cli-binary-format parameter to raw-in-base64-out.

Once I correct the JSON string and remove the dangling comma, the invoke command runs successfully.

shell
aws lambda invoke --function-name testFunction --cli-binary-format raw-in-base64-out --payload '{"name": "John Doe"}' response.json

successful invocation

Similarly, if we invoke the Lambda function with syntactically incorrect JSON stored in a file, we still get the "Could not parse request body into JSON" error.

shell
aws lambda invoke --function-name testFunction --cli-binary-format raw-in-base64-out --payload file://event.json response.json

file payload could not parse error

The contents of the file supplied to the --payload parameter can be any JSON with syntax errors, i.e.:

event.json
{ 'name': 'John Doe' }

In this case enclosing the key and value in double, instead of single quotes solves the error.

event.json
{ "name": "John Doe" }

Once the supplied JSON is syntactically correct, the error is resolved.

# Further Reading

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.