An HTTP trigger provides an HTTP(S) endpoint for a function, referred to as a function URL, allowing you to invoke the function directly via an HTTP request. This topic describes how to use an HTTP trigger to invoke a function in a built-in runtime of Function Compute. For information on using the trigger in a custom runtime, see Web functions.
Considerations
In Function Compute 3.0, the mechanism for HTTP triggers invoking functions that run in custom runtimes or Custom Container runtimes remains the same as in Function Compute 2.0. However, there are notable differences in how HTTP triggers invoke functions that run in built-in runtimes. For more information, see the following section.
Invocation process
In a built-in runtime, when a client calls a function URL, Function Compute maps the request to an event object (event) and then passes the event object to the function. After the function execution is complete, Function Compute maps the result to an HTTP response and sends it back to the client.
Request struct
Request struct format
The request struct format is as follows:
{
"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 currently supported version is v1. | v1 |
rawPath | The request path. For example, if the request URL is | /example |
body | The request body. During the mapping process, Function Compute performs Base64-encoding of binary data. | Hello FC! |
isBase64Encoded | Specifies whether the request body is Base64-encoded. Valid values: true and false. | false |
headers | The list of request headers, displayed in key-value pairs. If a key has multiple values, those values are separated by commas (,). The mapping process in which Function Compute 3.0 transforms the HTTP request into an event object results in the first letter of each request header key being converted to uppercase, a change known as the normalization process. For more information, see Why does the first letter of the header key become uppercase when I use an HTTP trigger to invoke a function? | {"Header1": "value1", "Header2": "value1,value2"} |
queryParameters | The request's 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 the requestId, the time when the request was made, and the identity of the authorized caller. | |
requestContext.accountId | The ID of the Alibaba Cloud account that owns the function. | 123456********* |
requestContext.domainName | The domain name of the HTTP trigger. | <http-trigger-id>.<region-id>.fcapp.run |
requestContext.domainPrefix | The domain prefix of the HTTP trigger. | <http-trigger-id> |
requestContext.http | An object that contains detailed information about the 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 makes the request. This IP address refers to the peer IP address (RemoteAddr) that directly establishes the connection, specifically the address of the client connecting directly to the server or the address of the last proxy that the client uses.
| 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 trace 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 |
Request mapping logic
Function Compute maps the incoming HTTP request into an event object before passing the event object to the handler of your function. The mapping logic is as follows:
The headers of the HTTP request map to
eventheaders.The query parameters of the HTTP request map to
queryParameters.Contextual information about the HTTP request maps to
requestContext.The request body of a POST request maps to
body.
Base64 encoding mechanism
When Function Compute maps an HTTP request to an event object (event), it checks the value of Content-Type in the request header to determine whether Base64 encoding should be performed.
If the value of
Content-Typeindicates a textual representation, Function Compute does not perform Base64 encoding, and theisBase64Encodedparameter ineventis set tofalse.Otherwise, the request body is converted into a text format using Base64 encoding, and the
isBase64Encodedparameter is set totrue.
The following Content-Type values indicate a textual representation:
text/*
application/json
application/ld+json
application/xhtml+xml
application/xml
application/atom+xml
application/javascript
Request mapping examples
GET
HTTP request | Event object |
| |
You can enter the following command in the CLI to send the HTTP request mentioned above. Replace https://example.cn-hangzhou.fcapp.run with the actual URL of your function.
curl -v "https://example.cn-hangzhou.fcapp.run?parameter1=value1¶meter2=value2"POST
HTTP request | Event object |
| |
You can enter the following command in the CLI to send the HTTP request mentioned above. Replace
https://example.cn-hangzhou.fcapp.runwith the actual URL of your function.curl -v -H "Content-Type: application/json" -d '{"message": "Hello"}' "https://example.cn-hangzhou.fcapp.run"If you want to encode your request into Base64 format, simply set
Content-Typein the request header toapplication/x-www-form-urlencoded.
Response struct
Response struct format
The response struct format is as follows. Function Compute parses the output of your function, transforms it into a response struct, and maps this struct to an HTTP response.
{
"statusCode": 200,
"headers": {
"Content-Type": "application/json",
"Custom-Header-1": "Custom Value"
},
"isBase64Encoded": false,
"body": "{\"message\":\"Hello FC!\"}"
}Response mapping logic
After parsing the function output, Function Compute maps the response struct to an HTTP response.
If your function returns valid JSON with a
statusCode, the mapping process is governed by the following rules:The
statusCodein the JSON maps to thestatusCodein the HTTP response.The
Content-Typein the JSON maps to theContent-Typein the HTTP response. If the JSON does not contain aContent-Typeparameter, default valueapplication/jsonis used.The
bodyin the JSON, which is the function output, maps to the HTTP responsebody.The
isBase64Encodedparameter in the JSON maps to theisBase64Encodedparameter in the HTTP response. If the JSON does not contain anisBase64Encodedparameter, default value "false" is used.
If your function returns valid JSON without a
statusCode, or if it returns data in a format other than JSON, Function Compute makes the following assumptions to construct the HTTP response:statusCodeis 200.Content-Typeisapplication/json.bodyis the function output.isBase64Encodedis false.
To conclude, Function Compute converts the data returned by your function into an HTTP response and sends it back to the client.
statusCodemaps to the status code of the HTTP response.headersmap to the HTTP response headers.bodymaps to the HTTP response body. If theisBase64Encodedparameter exists and its value is true, then thebodydata will be decoded into binary form before being mapped to the HTTP response body.
Response mapping examples
This section shows how your function output is parsed into a response struct and how that struct maps to the HTTP response on the client. When a client invokes your function through its HTTP(S) endpoint, it receives the HTTP response.
Output for a string response
Function output | Parsed function output | HTTP response (to the client) |
| | |
Output for a JSON response
Function output | Parsed function output | HTTP response (to the client) |
| | |
Output for a custom response
Function output | Parsed function output | HTTP response (to the client) |
| | |
Base64 decoding mechanism
If your function returns valid JSON with an isBase64Encoded parameter whose value is true, Function Compute performs Base64 decoding on the JSON body and then maps the decoded data to the HTTP response body.
If the JSON body fails to be decoded, Function Compute does not report an error. Instead, it directly returns the value of the JSON body to the client.
HTTP response headers
In a function URL invocation, the response header X-Fc-Request-Id, which is automatically added by Function Compute, uniquely identifies the request that triggered the function invocation. Function Compute does not automatically add any other response headers.
You can return custom headers from your functions. Note that custom headers that start with X-Fc- are not supported. Additionally, the following response headers cannot be used as custom headers because they are reserved by Function Compute.
connection
content-length
date
keep-alive
server
content-disposition
If any of these reserved headers is used as a custom header, Function Compute will simply ignore it.
Error handling
When a function invoked via an API encounters an error, the error message is returned as part of the response, and an HTTP 200 status code is returned. For example, the following sample code shows the response to a ModuleNotFound error in Python:
{
"errorMessage": "Unable to import module 'index'",
"errorType": "ImportModuleError",
"stackTrace": [
"ModuleNotFoundError: No module named 'not_exist_module'"
]
}However, in the case of function URL invocations, Function Compute hides the error message and only returns Internal Server Error. The HTTP status code is 502. The following sample code shows an example:
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 ErrorYou can use the X-Fc-Request-Id response header to efficiently pinpoint relevant logs and identify error messages associated with a request.
References
If you are writing code in a built-in runtime of Function Compute 3.0, you may find the following topics on handlers useful.