Reputation: 49
I'm trying to create function in NODE.js on AWS.Lamda-service, and test at function console works fine , but it can't be exported outside when the api would be called, api gateway error “Malformed Lambda proxy response” status error 502
The code is as bellow:
exports.handler = (event, context, callback) => {
event.options = {
'method': 'GET',
'hostname': 'app.respond.io',
'path': '/api/v1/contact/YXM2UJRaFBEdz09',
'headers': {
'Authorization': 'Bearer 529485f027b408ae91799eec57a53e0ac4',
'Content-Type': 'Application/json'
},
'maxRedirects': 20
};
const req = https.request(event.options, (res) => {
let body = '';
console.log('Status:', res.statusCode);
console.log('Headers:', JSON.stringify(res.headers));
res.setEncoding('utf8');
res.on('data', (chunk) => body += chunk);
res.on('end', () => {
console.log('Successfully processed HTTPS response');
if (res.headers['content-type'] === 'application/json') {
body = JSON.parse(body);
body = body.data.custom_fields.discount;
body = body.replace("%","");
}
callback(null, body);
});
});
req.on('error', callback);
req.write(JSON.stringify(event.data));
req.write(JSON.stringify(event.data));
req.end();
};
any idea
Upvotes: 1
Views: 1961
Reputation: 49
Finally I solved this issue.
1.I followed @Kulasangar instruction... and part of the problem I solved with his answer... So Malformed Lambda proxy response status error 502, it means that you cant export function response out of AWS Lambda.
One of the solutions is to uncheck Use Lambda Proxy integration in API Integration Request and do some changes to Mapping Templates... It is described Amazon API Gateway-Documentation. For the beginners it is better to create API from the link before then to create from the designer console. So with this changes I was granted to export response outside from AWS Lambda..
2.the second issue I had, it was "The first argument must be of type string or an instance of Buffer error", an error, when the response was exported outside AWS Lambda... so I did change the following lines :
// from:
//req.write(JSON.stringify(event.data));
//req.write(JSON.stringify(event.data));
// to:
req.write(JSON.stringify(event));
req.write(JSON.stringify(event));
Before all of this I have made some settings in IAM permission to API Gateway when I was trying to solve the problem, but I'm not sure which one could be related with the errors above... so if someone still has a problem should check about this settings...
So the final code is:
var https = require('https');
/**
* Pass the data to send as `event.data`, and the request options as
* `event.options`. For more information see the HTTPS module documentation
* at https://nodejs.org/api/https.html.
*
* Will succeed with the response body.
*/
exports.handler = (event, context, callback) => {
event.options = {
'method': 'GET',
'hostname': 'app.respond.io',
'path': '/api/v1/contact/YXM2cTRaktpVFBEdz09',
'headers': {
'Authorization': 'Bearer 52948ae917902a15e8ff2d0d2b816e1046497a53e0ac4',
'Content-Type': 'Application/json'
},
'maxRedirects': 20
};
const req = https.request(event.options, (res) => {
let body = '';
console.log('Status:', res.statusCode);
console.log('Headers:', JSON.stringify(res.headers));
res.setEncoding('utf8');
res.on('data', (chunk) => body += chunk);
res.on('end', () => {
console.log('Successfully processed HTTPS response');
// If we know it's JSON, parse it
if (res.headers['content-type'] === 'application/json') {
body = JSON.parse(body);
body = body.data.custom_fields.discount;
body = body.replace("%","");
}
const response = {
'statusCode': 200,
'body': body //,
//'isBase64Encoded': false
};
callback(null, JSON.stringify(response);
});
});
req.on('error', callback);
//req.write(JSON.stringify(event.data));
//req.write(JSON.stringify(event.data));
req.write(JSON.stringify(event));
req.write(JSON.stringify(event));
req.end();
};
Upvotes: 0
Reputation: 9454
I think this error pops up due to your response being not formatted correctly or rather the callback is not being set at the end. You need to have your response as below:
var response = {
"statusCode": 200,
"headers": {
"my_header": "my_value"
},
"body": JSON.stringify(responseBody),
"isBase64Encoded": false
};
callback(null, response);
You can also refer this SO, which has explanations for the same issue.
Upvotes: 1