Q:

call an external api using apigateway

I'm writing a lambda in node.js that will call an api(post) and gives back the resulting body and the code is as below.
const AWS = require('aws-sdk');
const request = require('request');

exports.handle = function(e, ctx, callback) {
    var bodyDetails = {
        uri: "myURL",
        json: {
            "requestChannel": "web1" },
        "method": "POST"
    };

    callback = ctx.done;
    var data = e.bodyJson || {};
    request(bodyDetails, function(error, response, body) {
        if (!error && response.statusCode === 200) {
            console.log(JSON.parse(JSON.stringify(body)));
            jsonBody = JSON.parse(JSON.stringify(body));
            console.log(body + "\t from suvccess") // Print the json response
            callback(null, jsonBody); // Return the JSON object back to our API call
        } else {
            callback(error);
        }
    });
}

and I'm testing the same in my lambda console. by passing a blank json {} and I get back the correct response. 
Now my next plan is to integrate this piece against API Gateway. So I've created an api for this in my apigateway and in that, I've created a resource named home. and in the home, I created a GET method. with the below details.
Integration type: Lambda Function
Use Lambda Proxy integration : checked
Lambda Region: us-east-1 
Lambda Function: myWorkingLambdaName

when I tested this using the test option given by apigateway. I get the response as 
Request: /home
Status: 502
Latency: 2942 ms
Response Body
{
  "message": "Internal server error"
}

when I see my console I see the values of the success block printed, but the status code is 502. This is very confusing, please let me know where am I going wrong and how can I fix this.
Thanks

A:

API Gateway expects the following properties to be returned from your Lambda:
{
    "isBase64Encoded": true|false,
    "statusCode": httpStatusCode,
    "headers": { "headerName": "headerValue", ... },
    "body": "..."
}

So, instead of callback(null, jsonBody), you should be calling callback like this:
callback(null, {
    isBase64Encoded: false,
    statusCode: 200,
    headers: {
        "Access-Control-Allow-Origin" : "*", 
    },
    body: JSON.stringify(jsonBody),
})

