"message" : "Internal server error" issue with Lambda/API Gateway and iOS

17,989

Solution 1

This may happen because your Lambda function is not set to return a HTTP status code.

Changing from

exports.handler = (event, context, callback) => {
    callback(null, 'Hello from Lambda');
};

to

exports.handler = (event, context, callback) => {
    callback(null, { statusCode: 200, body: 'Hello from Lambda' });
};

Should fix the issue.

Solution 2

I had the same issue with the following code:

exports.handler = async event => {
  console.log("hello world");
  return {
    statusCode: 200,
    body: event
  };
};

To fix all I had to do was JSON.stringify() the body.

exports.handler = async event => {
  console.log("hello world");
  return {
    statusCode: 200,
    body: JSON.stringify(event), // <-- here
  };
};

Solution 3

The JSON.stringify() solved my issue. The response.body needs to be in String format and not as JSON. I hope this helps.

exports.sendRes = (body, status = 200) => {
    var response = {
        statusCode: status,
        headers: {
            "Content-Type": "application/json"
        },
        body: JSON.stringify(body)
    };
    return response;
};
Share:
17,989
user3599895
Author by

user3599895

Updated on June 15, 2022

Comments

  • user3599895
    user3599895 almost 2 years

    I've set up a lambda function and created some GET and POST methods inside the API Gateway which seem to work fine when testing them inside the web application. I am then trying to call the functions inside an iOS application which is set up using the mobile hub. The functions also work inside the testing facility via the mobile hub perfectly fine, however when I actually test the functions inside the app I get:

    "message" : "Internal server error"
    

    I know the error is not much to work from, but I can't figure out a way to get a more detailed error description.

    Any ideas?

  • Pranjal Gupta
    Pranjal Gupta about 6 years
    Can you explain me what does ' { statusCode: 200, body: 'Hello from Lambda' } ' mean ? What if I replace this part of success message in callback to display some variable like- let a= 10; and then callback(null, a);
  • Ricardo Mayerhofer
    Ricardo Mayerhofer about 6 years
    This part is the Lambda function invocation response. If you replace it by "let a", then "a" will be the function response.
  • zhibirc
    zhibirc almost 6 years
    This return format only matters when using the API Gateway-Lambda combination in proxy mode. If you do not check the proxy option, then you can return whatever you’d like, the API gateway will forward it on.
  • Hashir Baig
    Hashir Baig almost 5 years
    It took half of my day and after seeing your answer, I remembered I had previously had the same issue got resolved by the same solution. Thanks a lot. I was using python btw and str(body) did the work.