我正在执行一个简单的api集成,集成了lambda和api网关。
const axios = require('axios');
const url = require("url");
exports.handler = async (event, context) => {
// TODO implement
var maxRJ = event.queryStringParameters.maxResults;
var jiraLabelJ = event.queryStringParameters.labels;
var statusJ = event.queryStringParameters.status;
const config = {
headers: {
"Accept": "application/json",
"Accept-Encoding": "gzip,deflate,compress",
"Authorization": "XXXXXXXX"
}
};
const queryParams = {
jql: "status="+statusJ+" AND labels="+jiraLabelJ+"",
maxResults: maxRJ,
};
const params = new url.URLSearchParams(queryParams);
console.log(params);
const output = axios.get(`apiURL?${params}`, config)
.then(response => {
return(JSON.stringify(response.data));
})
.catch(error => {console.error(error)
return(JSON.stringify(error));
});
return output;
};
上面的lambda通过LAMBDA_PROXY与api网关集成。但是在测试api网关集成时,我可以看到api响应,但是api网关返回502
Response Body
{"message": "Internal server error"}
Wed Dec 14 15:29:17 UTC 2022 : Execution failed due to configuration error: Malformed Lambda proxy response
Wed Dec 14 15:29:17 UTC 2022 : Method completed with status: 502
.
输出/测试集成显示日志-
Wed Dec 14 15:29:17 UTC 2022 : Endpoint response body before transformations: {api response}
在promise解析之前返回一个响应。你应该await
你的API调用,并返回一个代理响应@AnkushJain评论。
const axios = require('axios');
const url = require("url");
exports.handler = async (event, context) => {
// TODO implement
const maxRJ = event.queryStringParameters.maxResults;
const jiraLabelJ = event.queryStringParameters.labels;
const statusJ = event.queryStringParameters.status;
const config = {
headers: {
"Accept": "application/json",
"Accept-Encoding": "gzip,deflate,compress",
"Authorization": "XXXXXXXX"
}
};
const queryParams = {
jql: "status="+statusJ+" AND labels="+jiraLabelJ+"",
maxResults: maxRJ,
};
const params = new url.URLSearchParams(queryParams);
console.log(params);
try {
const output = await axios.get(`apiURL?${params}`, config)
return {
statusCode: 200,
body: JSON.stringify(output.data)
}
} catch(error) {
console.error(error)
return {
statusCode: 500,
body: JSON.stringify({
message: error.message
})
}
}
};
jql:"状态="+ statusJ +";和标签="+ jiraLabelJ +",
这是一个安全问题。不要把用户提供的输入直接放到JQL语句中。