An HTTP trigger provides HTTP and HTTPS URLs for a function. You can directly invoke a function by using the URLs provided by the HTTP trigger. This topic describes how use an HTTP trigger to invoke a function in a standard runtime.
Usage notes
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 2.0. However, HTTP triggers for functions that run in standard runtimes are significantly different from HTTP triggers 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 calls 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 URL. For example, if the request URL is | /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 | This parameter is a list of request headers. Values of this parameter are displayed in key-value pairs. If a key has multiple values, separate the values with commas (,). When you use an HTTP trigger to invoke a function that runs in a standard runtime, Function Compute 3.0 converts the HTTP requests to the event format of the HTTP trigger and normalizes keys of the HTTP request headers. In this case, the first letters of header keys are capitalized. For more information, see Why is the initial letter of a header key capitalized when I call an HTTP trigger? | {"Header1": "value1", "Header2": "value1,value2"} |
queryParameters | The request query parameters. For example, if the request URL is | { "parameter1": "value1", "parameter2": "value1,value2" } |
requestContext | An object that contains additional information about the request, such as requestId, the time of the request, and the identity of the authorized caller. | |
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 for the HTTP trigger of the function. | <http-trigger-id> |
requestContext.http | An object that contains details about an HTTP request. | |
requestContext.http.method | The HTTP method used in the 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 | /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 initiates the request. This IP address is the peer IP address (RemoteAddr) that directly establishes the connection, specifically, the address of the client that directly connects to the server, or the IP address of the last proxy.
| 11.11.XX.XX |
requestContext.http.userAgent | The value of the user-agent request header. | 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 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 ofstatusCode
in the JSON object.Content-Type
: The function returns the value ofContent-Type
in the JSON object. By default, if the JSON object does not containContent-Type
,Content-Type
isapplication/json
.body
: The response body. The function returns the value ofbody
in the JSON object.isBase64Encoded
: The function returns the value ofisBase64Encoded
in the JSON object. If there is noisBase64Encoded
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 isapplication/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. IfisBase64Encoded
exists and the value is true,body
is Base64-decoded and then mapped to the HTTP response body.
Example
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 |
|
|
|
JSON outputs
Function output | Parsing function output | HTTP response to clients |
|
|
|
Custom response output
Function output | Parsing function output | HTTP response to clients |
|
|
|
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 returns Internal Server Error
with an HTTP return code of 502
. The following sample code provides an example of an HTTP response:
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: