Could not parse request body into json: Unrecognized token

Viewed 11577

I have a very simple AJAX code that's calling an AWS API gateway endpoint:

$.ajax({
        url: 'https://omitted.execute-api.ap-southeast-2.amazonaws.com/test/rec',
        type: 'post',
        data: {
            'zipcode': '1234',
            'url': 'www.google.com'
        },
        dataType: 'json',
        success: function (data) {
            console.info(data);
        }
    });

And what I am getting back is:

Could not parse request body into json: Unrecognized token 'zipcode': was expecting ('true', 'false' or 'null')`

The data should be in JSON format so What am I doing wrong?

I have also tried:

$.post('https://omitted.execute-api.ap-southeast-2.amazonaws.com/test/rec',
    {
        'zipcode': '1234',
        'url': 'www.google.com'
    }, 
    function(data, textStatus) {
      //data contains the JSON object
      //textStatus contains the status: success, error, etc
}, "json");

$.post('https://omitted.execute-api.ap-southeast-2.amazonaws.com/test/rec',
    'zipcode=1234&url=www.google.com', 
    function(data, textStatus) {
      //data contains the JSON object
      //textStatus contains the status: success, error, etc
}, "json");

And they are returning the same result.

5 Answers

I changed the integration request to Proxy integration (in API GW Choose the method, go to integration request window and choose "Use Lambda Proxy integration") and it works!

Related