AWS API Gateway: Use Mock Integration to echo response body

爷,独闯天下 提交于 2020-12-29 12:24:11

问题


I am trying to use the Mock integration to just "echo" back me the JSON body that I sent. However, I just can't get it to work. I can return any arbitrary JSON body from my "POST - Integration Response", but the request JSON is never found via $input.json('$'). I have been able to successfully echo back any query parameters.

My API has a single "/" path and a single method defined (POST). The exported yaml is here .

Any idea as to what may be going on?


回答1:


Unfortunately this is not supported. In the mapping template for "Integration Response", $input represents the payload received from the integration response (which is empty in the case of a MOCK integration.




回答2:


I've found this to actually be possible, although a little hacky. First, in the integration request mapping template you store the body in a path parameter.

#set($context.requestOverride.path.body = $input.body)
{
  "statusCode": 200,
}

Then in the integration response mapping template you fetch it back and return it.

#set($body = $context.requestOverride.path.body)
{
  "statusCode": 200,
  "body": $body,
}

This seems to even work well with larger payloads.




回答3:


You can set up a lambda function for echoing purposing, something like that:

exports.handler = async (event) => {
    return event;
};


来源:https://stackoverflow.com/questions/47918477/aws-api-gateway-use-mock-integration-to-echo-response-body

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!