Last updated: Feb 26, 2024
Reading time·2 min
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.
aws lambda invoke --function-name testFunction --cli-binary-format raw-in-base64-out --payload '{"name": "John Doe",}' response.json
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.
--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.
aws lambda invoke --function-name testFunction --cli-binary-format raw-in-base64-out --payload '{"name": "John Doe"}' response.json
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.
aws lambda invoke --function-name testFunction --cli-binary-format raw-in-base64-out --payload file://event.json response.json
The contents of the file supplied to the --payload
parameter can be any JSON
with syntax errors, i.e.:
{ 'name': 'John Doe' }
In this case enclosing the key and value in double, instead of single quotes solves the error.
{ "name": "John Doe" }
Once the supplied JSON is syntactically correct, the error is resolved.