All Products
Search
Document Center

2. Author functions

Last Updated: Aug 31, 2018

In this step we create a function for HTTP trigger. Functions for HTTP trigger requests return parameters as the response. You can copy and paste the code sample based on your selected programming language to the code editing area of the console.

Note: Function handlers with an HTTP trigger are different from common function handlers.

Node.js sample

  1. var getRawBody = require('raw-body')
  2. module.exports.handler = function (request, response, context) {
  3. // get request info
  4. getRawBody(request, function (err, data) {
  5. var params = {
  6. path: request.path,
  7. queries: request.queries,
  8. headers: request.headers,
  9. method: request.method,
  10. body: data,
  11. url: request.url,
  12. clientIP: request.clientIP,
  13. }
  14. // Add your own logic here
  15. // Set response
  16. var respBody = new Buffer.from(JSON.stringify(params));
  17. // var respBody = new Buffer( )
  18. response.setStatusCode(200)
  19. response.setHeader('content-type', 'application/json')
  20. response.send(respBody)
  21. })
  22. };

Python sample

  1. # -*- coding: utf-8 -*-
  2. import json
  3. HELLO_WORLD = b"Hello world!\n"
  4. def handler(environ, start_response):
  5. request_uri = environ['fc.request_uri']
  6. response_body = {
  7. 'uri':environ['fc.request_uri'],
  8. 'method':environ['REQUEST_METHOD']
  9. }
  10. # do something here
  11. status = '200 OK'
  12. response_headers = [('Content-type', 'text/json')]
  13. start_response(status, response_headers)
  14. # Python2
  15. return [json.dumps(response_body)]
  16. # Python3 tips: When using Python3, the str and bytes types cannot be mixed.
  17. # Use str.encode() to go from str to bytes
  18. # return [json.dumps(response_body).encode()]

Next step

Test functions.