3

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

3
  • You've shown everything about the AWS configuration, but have not shown the actual request being sent. It might not actually be JSON, even though you think it is. How is the request being sent (a browser, JavaScript, curl, or something else)? Can you use a tool to see exactly what is going out on the wire? Oct 29, 2015 at 20:24
  • Yes and indeed it was a problem with the request as you can see in my answer below. I was sending a json but should have sent a string of a json in correct formatting Oct 29, 2015 at 20:31
  • Here's a tutorial I made that might help out: github.com/mauerbac/lambda-gateway-twilio-demo Oct 29, 2015 at 22:01

2 Answers 2

7

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);
    }
 });


};
0

Set your parameter in string format ( no json and no array) :

I used http_build_query() function in my curl execution :

$curl = curl_init();

        curl_setopt_array($curl, array(
            CURLOPT_RETURNTRANSFER => 1,
            CURLOPT_URL => "https://your_url_here",
            CURLOPT_POST => 1,
            CURLOPT_SSL_VERIFYHOST => 0,
            CURLOPT_SSL_VERIFYPEER => 0,
            CURLOPT_POSTFIELDS => http_build_query(array(
                "key1" => "val1",
                "key2" => "val2",
            ))
        ));

        $resp = curl_exec($curl);
        print_r($resp);
        if (!curl_exec($curl)) {
            die('Error: "' . curl_error($curl) . '" - Code: ' . curl_errno($curl));
        }
        curl_close($curl);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.