All Products
Document Center

1. Create functions and HTTP triggers

Last Updated: Oct 18, 2018

This tutorial describes how to build web services based on HTTP triggers. Follow these steps to create functions that have HTTP triggers configured and write the function code.

Create functions with HTTP triggers configured

  1. Log on to the Function Compute console.

  2. Refer to Service operation to create a service. In this tutorial, the name of the service is “web-server” and the region is East China 2 (Shanghai). Enable Advanced Settings for detailed setting.

  3. Click Log Project and LogStore to specify logging options. Select Create new role for Role Operation. Select AliyunLogFullAccess for System Policies. Click Authorize, and then click OK.

    • Logs for the functions are sent into the log store for debugging. For more information about logs, see Function logs.

    • This service is configured to run as a role that has access to other Alibaba Cloud services. For more information about permissions, see Permissions.

  4. Click Services in the left-side navigation pane, and select the service you have created.

  5. Refer to Function operation to create a function in the Service. On the Create Function page, specify the following configurations:

    1. Click Select All. Select nodejs8 or python2.7 from the drop-down list. Note: This tutorial shows the sample code in two languages.

    2. Select Empty Function.

    3. Configure the HTTP trigger as shown. Then click Next. For more information about HTTP triggers, see HTTP triggers.

      1. Select anonymous for Authorization. This option allows anonymous access without authentication.

      2. Select HTTP request methods that this function supports for Method.

        HTTP Trigger Configuration Sample

    4. Create the function and fill in Service Name, Function Name, Function Description, and Runtime.

    5. Click Next.

    6. Confirm your information and click Create.

Write the function code

Copy and paste the sample code to the editor in the console corresponding to the language you choose for the newly created function. All the function sample code return a JSON string containing the HTTP request parameter.

Node.js sample code

For more information about writing code in Node.js, seeNode.js Entry Point Configured with HTTP Trigger.

  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. // you can deal with 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 code

For more information about writing code in Python, see Python entry points configured with HTTP triggers.

  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()]

PHP sample code

For more information about writing code in PHP, see PHP entry points configured with HTTP triggers.

  1. <?php
  2. use RingCentral\Psr7\Response;
  3. function handler($request, $context): Response{
  4. /*
  5. $body = $request->getBody()->getContents();
  6. $queries = $request->getQueryParams();
  7. $method = $request->getMethod();
  8. $headers = $request->getHeaders();
  9. $path = $request->getAttribute("path");
  10. $requestURI = $request->getAttribute("requestURI");
  11. $clientIP = $request->getAttribute("clientIP");
  12. */
  13. return new Response(
  14. 200,
  15. array(
  16. "custom_header1" => "v1",
  17. "custom_header2" => ["v2", "v3"],
  18. ),
  19. "hello world"
  20. );
  21. }

What to do next

Resolve the domain name.