- Print
- PDF
Web action utilization
- Print
- PDF
Available in Classic and VPC
The examples of various formats, HTTP redirection, header cookie configuration, and image responses that may help with understanding and utilizing web actions are introduced. Example code is as follows.
JSON format examples
- When the type called is JSON
function main(params) { return {"key" : "value"} }
- When the type called is HTTP
function main(params) { return { statusCode: 200, headers: { 'Content-Type': 'application/json' }, body: {"key":"value"} }; }
- When the type called is JSON
HTML format examples
- When the type called is HTML
function main(params) { return {"html":"<html><body><h3>hello</h3></body></html>"} }
- When the type called is HTTP
function main() { return { headers: { 'Content-Type': 'text/html' }, statusCode: 200, body: '<html><body><h3>hello</h3></body></html>' } }
- When the type called is HTML
TXT format examples
- When the type called is TXT
function main(params) { return {"text":"hello"} }
- When the type called is HTTP
function main() { return { headers: { 'Content-Type': 'text/plain' }, statusCode: 200, body: 'hello' } }
- When the type called is TXT
HTTP redirection example
function main() { return { headers: { location: 'https://www.ncloud.com' }, statusCode: 302 } }
Examples of setting header cookie
function main() { return { headers: { 'Set-Cookie': 'UserID=Jane; Max-Age=3600; Version=', 'Content-Type': 'text/html' }, statusCode: 200, body: '<html><body><h3>hello</h3></body></html>' } }
function main() { return { headers: { 'Set-Cookie': [ 'UserID=Jane; Max-Age=3600; Version=', 'SessionID=asdfgh123456; Path = /' ], 'Content-Type': 'text/html' }, statusCode: 200, body: '<html><body><h3>hello</h3></body></html>' } }
Examples of image response: This example returns an image through a
base64
string by setting the content type toimage/png
.function main() { let png = <base 64 encoded string> return { headers: { 'Content-Type': 'image/png' }, statusCode: 200, body: png }; }
CautionSince there's a maximum response size that can be returned by an action, if there's an attempt to return a response value that exceeds the maximum size, the action's execution result is processed as a failure. So, be cautious of the maximum response size if you'd like to return an object file, such as the image in the example above. If it exceeds the maximum size, then don't return it as action code, but use a different storage. Refer to Prerequisites for using Cloud Functions for the maximum response size specifications.