How to return plain text from AWS Lambda & API Gateway
2017-02-20 21:18
323 查看
With limited experience in AWS Lambda & API Gateway, it's struggling to find the correct way to return plain text. Why plain text? It's plain text in the empty function template of Python runtime.
When watching the tutorial video about using AWS Lambda on Youtube, I think it's quite easy to run the hello world example. The videos use Node.js runtime which is friendly to JSON strings. In python, the function template just return plain text, not JSON. When finishing the Lambda part and running the test, everything was just fine.
It's the API Gateway to expose Lambda function for HTTP access. Adding a API Gateway trigger will result in a resource with ANY method. If openning the URL in a browser, it just complains a 500 error "Internal Server Error". After some searches and digging in the log, I found the calling of Lambda function was OK, but the result was not parsed correctly. To parse the result plain text, you have to add Content-Type in the response header. Also body mapping template has to be updated.
Key points
* Create a new GET method.
* In Method Response, add a header "Content-Type" and empty model.
* In Integration Response, add Content-Type and 'plain/text' (including the quote signs) in Header Mapping. In Body Mapping Template, add "plain/text" and "$input.path('$')".
[1] https://kennbrodhagen.net/2016/01/31/how-to-return-html-from-aws-api-gateway-lambda/
When watching the tutorial video about using AWS Lambda on Youtube, I think it's quite easy to run the hello world example. The videos use Node.js runtime which is friendly to JSON strings. In python, the function template just return plain text, not JSON. When finishing the Lambda part and running the test, everything was just fine.
It's the API Gateway to expose Lambda function for HTTP access. Adding a API Gateway trigger will result in a resource with ANY method. If openning the URL in a browser, it just complains a 500 error "Internal Server Error". After some searches and digging in the log, I found the calling of Lambda function was OK, but the result was not parsed correctly. To parse the result plain text, you have to add Content-Type in the response header. Also body mapping template has to be updated.
Key points
* Create a new GET method.
* In Method Response, add a header "Content-Type" and empty model.
* In Integration Response, add Content-Type and 'plain/text' (including the quote signs) in Header Mapping. In Body Mapping Template, add "plain/text" and "$input.path('$')".
[1] https://kennbrodhagen.net/2016/01/31/how-to-return-html-from-aws-api-gateway-lambda/
相关文章推荐
- How to use virtual path providers to dynamically load and compile content from virtual paths in
- How to eliminate the compile error "conversion from 'double ' to 'float ', possible loss of data"
- 通过findViewById()方法从layout中获取view并进行相应的转换时提示:"Cannot cast from View to AutoCompleteTextView"的解决办法!
- How to import data from a text file to a ACCESS table
- [Guide] Mac OS X Lion: How To Boot A Linux Live System From An USB Drive & How To Update Any OCZ SSD
- How to pass in/out return VB Byte array from a COM Component written in C#
- QString qs = ui.textEdit->toPlainText();
- How to allow a user to click on TextBlocks which return an integer ID in the click handler
- Amazon EC2 - how to get available ram and cpu usage via AWS API?
- Oracle EBS R12 how to call standard API/packages from custom form or reports
- How to search for a taglib's code (from wiki)
- vc++控件变量SetDlgItemText(): cannot convert parameter 1 from 'char [10]' to ‘
- From an inner class, "this" points to the inner object. How do I get a "this" pointer to the enclosing object?
- How to hide & unhide the grid from the page by using people code?
- prjGetWriteINIFile - How to Read-Write INI file using VB & API - 读取/写入INI文件 - VB6 + API - SourceCode - HackerJLY
- How to enable Plain Text Password
- how to create colored 3D Objects using the OpenGL® ES cross-platform API.
- How to use virtual path providers to dynamically load and compile content from virtual paths in
- The robot API &&How to add and use sensor.
- How to Migrate from WCF Web API to ASP.NET Web API