Zigglzworth Zigglzworth - 2 months ago 18
JSON Question

AWS API Gateway - Error when calling API method - 'Could not parse request body into json'

I have a simple POST method connected to a lambda function in my AWS API Gateway.

When I perform a test (via the Gateway API console) everything works fine and the test gets the results I am looking for. Its simple - post a JSON, get back a JSON.

However, after deploying the API and then sending the same JSON used in the test (via http post), I am getting 'Could not parse request body into json'.

Does anyone know what I may be doing wrong?
Note: I am not looking to use models, I just want to pass-through the JSON. I believe that when Amazon writes things like 'input passthrough'
they mean that the input can pass through to the lambda function.

Here are images of my AWS Gateway API setup:

METHOD REQUEST:
enter image description here

INTEGRATION REQUEST:
enter image description here

METHOD RESPONSE:
enter image description here

INTEGRATION RESPONSE:
enter image description here

Answer

I found the solution. The problem was that in the POST request you need to send your body as a string and not a JSON object and that string needs to be formatted correct

ie '{"key1": "val1","key2": 22,"key3": 15,"key4": "val4"}'

like so:

function post() {


$.ajax({
    url: "https://myapi.us-east-1.amazonaws.com/myStage/myPath",
    type: "POST",
    data: '{"key1": "val1","key2": 22,"key3": 15,"key4": "val4"}',
    mozSystem: true,
    dataType: "text", //set to"json" usually
    success: function (result) {
        switch (result) {
            case true:
                processResponse(result);
                break;
            default:
                resultDiv.html(result);
        }
    },
    error: function (xhr, ajaxOptions, thrownError) {
        alert('error ' + xhr.status + ' ' + thrownError);
        alert(thrownError);
    }
 });


};