All Products
Search
Document Center

Function Compute:Use an HTTP trigger to invoke a function

Last Updated:Apr 09, 2024

An HTTP trigger provides the HTTP and HTTPS URLs for a function. You can directly invoke a function by using the URL provided by the HTTP trigger. This topic describes how use an HTTP trigger to invoke a function in a standard runtime.

Precautions

In Function Compute 3.0, HTTP triggers for functions that run in custom runtimes and Custom Container runtimes are the same as HTTP triggers of Function Compute V2.0. However, HTTP triggers for functions that run in standard runtimes are significantly different from handlers of Function Compute 2.0. This topic describes how to use an HTTP trigger to invoke a function in a standard runtime.

Invocation process

When a client invokes a function URL, Function Compute maps the request to the event object event and passes event to the function. The function responses are then mapped to an HTTP response. Function Compute sends the HTTP response back to the client through the function URL.

Request struct

Request struct format

The following sample code shows the struct format of a request:

{
    "version": "v1",
    "rawPath": "/example",
    "body": "Hello FC!",
    "isBase64Encoded": false,
    "headers": {
        "header1": "value1",
        "header2": "value1,value2"
    },
    "queryParameters": {
        "parameter1": "value1",
        "parameter2": "value1,value2"
    },
    "requestContext": {
        "accountId": "123456*********",
        "domainName": "<http-trigger-id>.<region-id>.fcapp.run",
        "domainPrefix": "<http-trigger-id>",
        "http": {
            "method": "GET",
            "path": "/example",
            "protocol": "HTTP/1.1",
            "sourceIp": "11.11.11.**",
            "userAgent": "PostmanRuntime/7.32.3"
        },
        "requestId": "1-64f6cd87-*************",
        "time": "2023-09-05T06:41:11Z",
        "timeEpoch": "1693896071895"
    }
}

The following table describes the parameters:

Parameter

Description

Example

version

The payload format version of the event. The current version is v1.

v1

rawPath

The request path. For example, if the request URL is https://{url-id}.{region}.fcapp.run/example, the original path is /example. The value of the rawPath parameter is in URL-encoded format. To obtain the path after URL decoding, use the requestContext.http.path parameter.

/example

body

The request body. If the content of the request is of the binary type, the body is Base64-encoded.

Hello FC!

isBase64Encoded

Set the value to true if the body is of the binary type and Base64 encoded. Otherwise, set the value to false.

false

headers

A list of request headers. Set the value to key-value pairs. If a key has multiple values, separate the values with commas (,), for example, {"header1": "value1", "header2": "value1,value2"}.

{"header1": "value1", "header2": "value1,value2"}

queryParameters

The query parameters of the request. For example, if the request URL is https://{url-id}.{region}.fcapp.run/example?key1=value1, the queryStringParameters value is a JSON object that contains a key of key1 and a value of value1. If a key has multiple values, separate the values with commas (,), for example, {"key1": "value1", "key2": "value2,value3"}.

{

"parameter1": "value1",

"parameter2": "value1,value2"

}

requestContext

An object that contains additional information about a request, such as the request ID, the time of the request, and the caller who passed the authorization.

requestContext.accountId

The ID of the Alibaba Cloud account who owns the function.

123456*********

requestContext.domainName

The domain name of the function HTTP trigger.

<http-trigger-id>.<region-id>.fcapp.run

requestContext.domainPrefix

The domain prefix of the function HTTP trigger.

<http-trigger-id>

requestContext.http

An object that contains details about an HTTP request.

requestContext.http.method

The HTTP method used in this request. Valid values: GET, POST, PUT, HEAD, OPTIONS, PATCH, and DELETE.

GET

requestContext.http.path

The request path. For example, if the request URL is https://{url-id}.{region}.fcapp.run/example?name=Jane, the path value is /example.

/example

requestContext.http.protocol

The protocol of the request.

HTTP/1.1

requestContext.http.sourceIp

The source IP address of the immediate TCP connection that makes the request.

11.11.XX.XX

requestContext.http.userAgent

The user-agent request header value.

PostmanRuntime/7.32.3

requestContext.requestId

The request ID. You can use this ID to track invocation logs related to the function.

1-64f6cd87-*************

requestContext.time

The timestamp of the request.

2023-09-05T06:41:11Z

requestContext.timeEpoch

The timestamp of the request, in Unix time.

1693896071895

Base64 encoding mechanism

When Function Compute maps an HTTP request to event of an event object, Function Compute determines whether to perform Base64 encoding on the HTTP body based on the value of Content-Type in the request header.

If the value of Content-Type indicates the text type, Function Compute does not perform Base64 encoding on the response body. Otherwise, the response body is Base64-encoded.

Content-Type values that indicate the text type:

  • text/*

  • application/json

  • application/ld+json

  • application/xhtml+xml

  • application/xml

  • application/atom+xml

  • application/javascript

Response struct

Response struct format

When a function returns a response, Function Compute parses the response and converts it into an HTTP response. The following sample code shows the format of a parsed response struct:

{
    "statusCode": 200,
    "headers": {
        "Content-Type": "application/json",
        "Custom-Header-1": "Custom Value"
    },
    "isBase64Encoded": "false",
    "body": "{\"message\":\"Hello FC!\"}"
}

Function Compute parsing logic

Function Compute parses responses and constructs an HTTP responses to return to clients.

  • If your function returns a valid JSON object that contains statusCode, Function Compute parses the response based on the following logic:

    • statusCode: The function returns the value of statusCode in the JSON object.

    • Content-Type: The function returns the value of Content-Type in the JSON object. By default, if the JSON object does not contain Content-Type, Content-Type is application/json.

    • body: The response body. The function returns the value of body in the JSON object.

    • isBase64Encoded: The function returns the value of isBase64Encoded in the JSON object. If there is no isBase64Encoded in the JSON object, the default value is false.

  • If your function returns a valid JSON object that does not contain statusCode, or no valid JSON object is returned, Function Compute makes the following assumptions to construct a response struct:

    • statusCode: The default value is 200.

    • Content-Type: The default value is application/json.

    • body: the response body, which is the data of return in the code.

    • isBase64Encoded: The default value is false.

Function Compute maps the function response struct to an HTTP response and returns it to the client based on the following logic:

  • statusCode is mapped to the status code of the HTTP response.

  • headers is mapped to the HTTP response header.

  • body is mapped to the HTTP response body. If isBase64Encoded exists and the value is true, body is Base64-decoded and then mapped to the HTTP response body.

Examples

This section provides examples of mapping function outputs to function response structs and HTTP responses. When a client invokes the function HTTP trigger, the HTTP response is returned.

Output of string responses

Function output

Parsing function output

HTTP response to clients

Hello World!

{
  "statusCode": 200,
  "body": "Hello World!",
  "headers": {
    "content-type": "application/json"
  },
  "isBase64Encoded": false
}
HTTP/1.1 200 OK
Content-Disposition: attachment
Content-Length: 12
Content-Type: application/json
X-Fc-Request-Id: 1-64f6d6e7-e01edb1cce58240ed59b59d9
Date: Tue, 05 Sep 2023 07:21:11 GMT

Hello World!

JSON outputs

Function output

Parsing function output

HTTP response to clients

{"message": "Hello World!"}

{
  "statusCode": 200,
  "body": "{\"message\": \"Hello World!\"}",
  "headers": {
    "content-type": "application/json"
  },
  "isBase64Encoded": false
}
HTTP/1.1 200 OK
Content-Disposition: attachment
Content-Length: 27
Content-Type: application/json
X-Fc-Request-Id: 1-64f6d867-7302fc1ac6338b6fd2adb782
Date: Tue, 05 Sep 2023 07:27:35 GMT

{"message": "Hello World!"}

Custom response output

Function output

Parsing function output

HTTP response to clients

{
   "statusCode": 201,
    "headers": {
        "Content-Type": "application/json",
        "My-Custom-Header": "Custom Value"
    },
    "body": {
        "message": "Hello, world!"
    },
    "isBase64Encoded": false
}
{
   "statusCode": 201,
    "headers": {
        "Content-Type": "application/json",
        "My-Custom-Header": "Custom Value"
    },
    "body": {
        "message": "Hello, world!"
    },
    "isBase64Encoded": false
}
HTTP/1.1 200 OK
Content-Disposition: attachment
Content-Length: 27
Content-Type: application/json
Custom-Header-1: Custom Value
X-Fc-Request-Id: 1-64f6dcb3-e787580749d3ba13b047ce14
Date: Tue, 05 Sep 2023 07:45:55 GMT

{"message": "Hello world!"}

Base64 decoding mechanism

If the function output is a valid JSON object and the value of isBase64Encoded is true, Function Compute performs Base64 decoding on body in the JSON object and returns the decoded data to the client by using the HTTP response body.

If body fails to be decoded, Function Compute does not report an error and directly returns the value of body to the client.

HTTP response headers

When you use an HTTP trigger to invoke a function, the response header contains the response header X-Fc-Request-Id that is automatically added by Function Compute. The response header is the unique identifier of a request. Except for X-Fc-Request-Id, Function Compute does not automatically add other response headers.

You can return custom response headers in the code. However, response headers that start with X-Fc- and the following reserved response headers of Function Compute are not supported:

  • connection

  • content-length

  • date

  • keep-alive

  • server

  • content-disposition

If you use a reserved header in a response header, Function Compute ignores the response header.

Error handling

When a function error is encountered, the error message is returned if the function is invoked by API calls. The HTTP return code is 200. For example, the following sample code is the response to the ModuleNotFound error in Python:

{
    "errorMessage": "Unable to import module 'index'",
    "errorType": "ImportModuleError",
    "stackTrace": [
        "ModuleNotFoundError: No module named 'not_exist_module'"
    ]
}

However, if the function is invoked by an HTTP trigger, Function Compute hides the error messages and directly return Internal Server Error with an HTTP return code of 502. The following sample code provides an example of an HTTP:

HTTP/1.1 502 Bad Gateway
Content-Disposition: attachment
Content-Type: application/json
X-Fc-Request-Id: 1-64f6df91-fe144d52e4fd27afe3d8dd6f
Date: Tue, 05 Sep 2023 07:58:09 GMT
Content-Length: 21

Internal Server Error

In this case, you can use X-Fc-Request-Id that is returned by the request to find the specific error message in the log.

Code development

Refer to the following topics about handlers if you write code in a standard runtime of Function Compute 3.0: