当我使用 GET 方法获取时,我收到错误 502
When I fetch using GET method I receive an error 502
当我尝试使用 GET 获取时收到错误 502,但是当我使用 POST 获取时收到响应。
拉姆达:
const headers = {'Content-Type':'application/json'}
exports.handler = async function(event) {
return {
statusCode: 200,
headers: {
headers
},
body: JSON.stringify({"ok":"ok"})
}
};
获取:
fetch("https://n3bvznv385.execute-api.us-east-2.amazonaws.com/dev/juegos", {
method: 'GET',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
'authorizationToken' : '123',
}
}).then(response => response.json())
.catch(error => console.error('Error:', error))
.then(response => console.log(JSON.parse(response.body)));
Error using GET
Success using POST
API 使用代理集成时来自 Lambda 的网关 expects the following response format:
{
"isBase64Encoded": true|false,
"statusCode": httpStatusCode,
"headers": { "headerName": "headerValue", ... },
"multiValueHeaders": { "headerName": ["headerValue", "headerValue2", ...], ... },
"body": "..."
}
但是,您将嵌套的 object 作为 headers 传递,因为您的代码解析为:
headers: { {'Content-Type':'application/json'} }
.
尝试像这样包含可能的 headers:
exports.handler = async function(event) {
return {
statusCode: 200,
headers: headers,
body: JSON.stringify({"ok":"ok"})
}
};
当我尝试使用 GET 获取时收到错误 502,但是当我使用 POST 获取时收到响应。
拉姆达:
const headers = {'Content-Type':'application/json'}
exports.handler = async function(event) {
return {
statusCode: 200,
headers: {
headers
},
body: JSON.stringify({"ok":"ok"})
}
};
获取:
fetch("https://n3bvznv385.execute-api.us-east-2.amazonaws.com/dev/juegos", {
method: 'GET',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
'authorizationToken' : '123',
}
}).then(response => response.json())
.catch(error => console.error('Error:', error))
.then(response => console.log(JSON.parse(response.body)));
Error using GET Success using POST
API 使用代理集成时来自 Lambda 的网关 expects the following response format:
{
"isBase64Encoded": true|false,
"statusCode": httpStatusCode,
"headers": { "headerName": "headerValue", ... },
"multiValueHeaders": { "headerName": ["headerValue", "headerValue2", ...], ... },
"body": "..."
}
但是,您将嵌套的 object 作为 headers 传递,因为您的代码解析为:
headers: { {'Content-Type':'application/json'} }
.
尝试像这样包含可能的 headers:
exports.handler = async function(event) {
return {
statusCode: 200,
headers: headers,
body: JSON.stringify({"ok":"ok"})
}
};