Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
870 views
in Technique[技术] by (71.8m points)

amazon web services - Return HTML from AWS API Gateway

I am trying to achieve the same thing as this post, but I do not understand the reply there and don't have permission to ask for clarification in the comments.

I have an API Gateway endpoint that accepts a GET request, passes through some request variables to the Lambda function (implemented in Python), and returns text/html via an Empty Response Model (as described here

As described in the earlier SO question, if the Lambda function returns an HTML string and the API endpoint uses the default Output Passthrough behavior @ Integration Response, the HTML output is quoted:

"
<html>
<body>
 ... 
</body>
</html>
"

That answer (by @ARUNBALAN NV) says "Simply store the HTML markup in a variable and return it.", but I am unsure what that means in the context of a Lambda function. Does it mean to return an "application/json" response with an element named "variableHTML"? Something like this?

"{"variableHTML": "\n<html>\n<body>\n ... \n</body>\n</html>\n"}"

I set that up & in API Gateway my Integration Response now uses a Mapping to extract the element (for 200 application/json responses) exactly as suggested:

#set($inputRoot = $input.path('$')) 
$inputRoot.variableHTML .

The result is now a single dot being returned.

I have tried many variations ($input.json instead of $input.path, different content types at different stages, etc), but feel the above setup most closely matches the accepted answer from the other thread.

Any insight where I am going wrong with this will be appreciated. Thanks for reading!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You're very close. The key to understanding this is realizing that whatever Python object you return will be serialized to JSON. So, if you return a string, it will be quoted and escaped to a valid JSON object. If you want the value of this string, then use the following Integration Response mapping:

#set($inputRoot = $input.path('$')) 
$inputRoot

The #set line gives $inputRoot the value of the entire JSON object your Python program returned... which is just the original string you returned before the Lambda framework converted it to JSON.

Suppose you wanted to build the response in the mapping, instead of in your program. Then, instead of returning a Python string, you could return a Python object, like so:

return {"title": "Greeting", "message": "Hello"}

Your mapping could convert that to HTML like so:

#set($inputRoot = $input.path('$')) 
<html><head><title>$inputRoot.title</title></head>
<body>$inputRoot.message</body></html>

Using a mapping that way is more useful if you're returning structured data than simple HTML, though. I'd use the first mapping above for your problem.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...