集成响应中的AWS API Gateway标头和主体映射 [英] AWS API Gateway Header and Body Mappings in Integration Response
问题描述
我正在尝试在集成响应中为API网关端点正确设置主体映射和标头映射.
I am trying to properly setup Body Mapping and Header Mapping in the Integration Response for an API Gateway endpoint.
在我们的Lambda中,我们有
In our Lambda we have
if (response.statusCode == 200) {
context.succeed(output);
} else if (response.statusCode == 206) {
var paginationObject = {
errorType : "PartialContent",
errorCode : 206,
detailedMessage : "PartialContent Returned",
stackTrace : [],
data : {
output
}
};
context.fail(JSON.stringify(paginationObject));
}
然后我使用Lambda错误正则表达式.* PartialContent.* 在集成响应中获取此信息,并将人体贴图模板作为
I then handle fetching this in the Integration Response using a Lambda Error Regex of .*PartialContent.* and have my Body Mapping Template as
#set($allParams = $input.params())
#set($body = $util.parseJson($input.json('$.errorMessage')))
$body
这为我提供了正确的HTTP状态代码和JSON输出,但是主体中的数据过多.响应如下:
This gives me the correct HTTP status code and JSON output, but it has too much data in the body. The response looks like:
{
"errorType":"PartialContent",
"errorCode":206,
"detailedMessage":"PartialContent Returned",
"stackTrace":[],
"data":{
"output":{
"status":206,
"bodyJson":[{"call_date":"2017-08-19 18:17:21"}],
"headers":{"date":"Thu, 02 Nov 2017 18:36:52 GMT",
"server":"Apache",
"x-pagination-page-size":10}
}
}
}
我希望标题实际上在响应中显示为标题,并且我希望正文只是bodyJson内部的内容
I want the headers to actually appear as headers in the response, and I want the body to just be the content inside of bodyJson
我试图将主体映射模板更改为使用 $ body.data.output.bodyJson ,但是当我这样做时,主体完全为空.我还在标题映射器中设置了标题,同时尝试 integration.response.body.headers.x-pagination-page-size 和 integration.response.header.x-pagination -page-size ,但两次标题都为空,即使我在JSON输出中可以看到正确的值.
I've tried to change the body mapping template to use $body.data.output.bodyJson, but when I do that the body is completely empty. I've also got the headers set in the Header Mappers, trying both integration.response.body.headers.x-pagination-page-size and integration.response.header.x-pagination-page-size but both times the header is blank, even though I can see the proper values in the JSON output.
如何仅将bodyJson元素作为响应的主体输出?以及如何正确地映射标题?
How do I get just the bodyJson element to be output as the body of the response? And how do I properly get the headers mapped?
推荐答案
您是否尝试在集成响应中使用 Lambda错误正则表达式? 例如:
Did you try to use Lambda Error Regex in Integration Response? For example:
.*状态":400.*
.*"status":400.*
人体贴图模板:
#set ($errorMessageObj = $util.parseJson($input.path('$.errorMessage')))
{
"status" : "$errorMessageObj.status",
"errorType" : "$errorMessageObj.errorType",
"message" : "$errorMessageObj.errorMessage"
}
我在 Lamda 中创建了错误功能:
function error(status, errorType, errorMessage, callback){
callback(JSON.stringify({
status: status,
errorType: errorType,
errorMessage: errorMessage
}));
}
用法:
error(404, "Not Found", "Resource is not found", callback);
这篇关于集成响应中的AWS API Gateway标头和主体映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!