An HTTP trigger exposes a function as an HTTP(S) endpoint, called a function URL. When a client calls the function URL, Function Compute converts the HTTP request into an event object and passes it to your function handler. After the function returns, Function Compute maps the output back to an HTTP response and sends it to the client.
This topic covers HTTP trigger behavior in built-in runtimes. For custom runtimes, see Web functions.
In Function Compute 3.0, HTTP trigger behavior in built-in runtimes differs notably from Function Compute 2.0. For custom runtimes and Custom Container runtimes, the behavior remains the same as in Function Compute 2.0.
How it works
When a client calls your function URL:
Function Compute maps the HTTP request to an event object (
event).The event object is passed to your function handler.
After your function returns, Function Compute maps the output to an HTTP response and sends it back to the client.
Request struct
Format
Function Compute maps the incoming HTTP request to an event object with the following structure:
{
"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"
}
}Parameters
| Parameter | Description | Example |
|---|---|---|
version | Payload format version. The only supported value is v1. | v1 |
rawPath | URL-encoded request path. For a request URL like https://{url-id}.{region}.fcapp.run/example, this value is /example. For the decoded path, see requestContext.http.path. | /example |
body | Request body. Binary data is Base64-encoded. | Hello FC! |
isBase64Encoded | Whether the request body is Base64-encoded. Valid values: true, false. | false |
headers | Request headers as key-value pairs. If a key has multiple values, they are separated by commas. In Function Compute 3.0, the first letter of each header key is uppercased (normalization). 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 | Query parameters as a JSON object. For a URL like https://{url-id}.{region}.fcapp.run/example?key1=value1, this value is {"key1": "value1"}. Multiple values for the same key are separated by commas. | {"parameter1": "value1", "parameter2": "value1,value2"} |
requestContext | Additional request metadata, including the request ID, timestamp, and caller information. | — |
requestContext.accountId | ID of the Alibaba Cloud account that owns the function. | 123456********* |
requestContext.domainName | Domain name of the HTTP trigger. | <http-trigger-id>.<region-id>.fcapp.run |
requestContext.domainPrefix | Domain prefix of the HTTP trigger. | <http-trigger-id> |
requestContext.http.method | HTTP method. Valid values: GET, POST, PUT, HEAD, OPTIONS, PATCH, DELETE. | GET |
requestContext.http.path | Decoded request path. | /example |
requestContext.http.protocol | Request protocol. | HTTP/1.1 |
requestContext.http.sourceIp | Peer IP of the direct TCP connection (RemoteAddr). See the note below. | 11.11.XX.XX |
requestContext.http.userAgent | Value of the user-agent request header. | PostmanRuntime/7.32.3 |
requestContext.requestId | Request ID for tracing invocation logs. | 1-64f6cd87-************* |
requestContext.time | Request timestamp in ISO 8601 format. | 2023-09-05T06:41:11Z |
requestContext.timeEpoch | Request timestamp in UNIX time (milliseconds). | 1693896071895 |
sourceIp is the peer IP of the direct TCP connection, not necessarily the original client IP. If the request is not forwarded by a proxy, sourceIp is the client's IP. If the request passes through one or more proxies, sourceIp is the IP of the last proxy. To get the original client IP when requests go through proxies, read the X-Forwarded-For header. For details, see How do I obtain the original IP address of a client when an HTTP trigger invokes a function that uses a built-in runtime?
Mapping logic
Function Compute maps the HTTP request to the event object as follows:
HTTP request headers →
event.headersHTTP query parameters →
event.queryParametersRequest context (request ID, timestamp, caller identity) →
event.requestContextPOST request body →
event.body
Base64 encoding
Function Compute checks the Content-Type header to decide whether to Base64-encode the request body.
Content-Type | isBase64Encoded | Body handling |
|---|---|---|
text/* | false | Passed as-is |
application/json | false | Passed as-is |
application/ld+json | false | Passed as-is |
application/xhtml+xml | false | Passed as-is |
application/xml | false | Passed as-is |
application/atom+xml | false | Passed as-is |
application/javascript | false | Passed as-is |
| Any other value | true | Base64-encoded before passing to function |
Request mapping examples
GET
| HTTP request | Event object |
|---|---|
GET /?parameter1=value1¶meter2=value2 HTTP/1.1 | {"version":"v1","rawPath":"/","headers":{"Accept":"*/*","User-Agent":"CurlHttpClient"},"queryParameters":{"parameter1":"value1","parameter2":"value2"},"body":"","isBase64Encoded":true,"requestContext":{"accountId":"1327**********","domainName":"example.cn-hangzhou.fcapp.run","domainPrefix":"example","requestId":"1-67aee50c-****-**********","time":"2025-02-14T06:39:08Z","timeEpoch":"1739515148145","http":{"method":"GET","path":"/","protocol":"HTTP/1.1","sourceIp":"40.XX.XX.XX","userAgent":"CurlHttpClient"}}} |
To send this request from the CLI (replace https://example.cn-hangzhou.fcapp.run with your function URL):
curl -v "https://example.cn-hangzhou.fcapp.run?parameter1=value1¶meter2=value2"POST
| HTTP request | Event object |
|---|---|
POST / HTTP/1.1<br>Content-Type: application/json | {"version":"v1","rawPath":"/","headers":{"Accept":"*/*","Content-Length":"20","Content-Type":"application/json","User-Agent":"curl/8.7.1"},"queryParameters":{},"body":"{\"message\": \"Hello\"}","isBase64Encoded":false,"requestContext":{"accountId":"1327**********","domainName":"example.cn-hangzhou.fcapp.run","domainPrefix":"example","requestId":"1-67aee50c-****-**********","time":"2025-02-14T06:39:08Z","timeEpoch":"1739515148145","http":{"method":"POST","path":"/","protocol":"HTTP/1.1","sourceIp":"40.XX.XX.XX","userAgent":"CurlHttpClient"}}} |
To send this request from the CLI (replace https://example.cn-hangzhou.fcapp.run with your function URL):
curl -v -H "Content-Type: application/json" -d '{"message": "Hello"}' "https://example.cn-hangzhou.fcapp.run"To force Base64 encoding of the request body, setContent-Typetoapplication/x-www-form-urlencoded.
Response struct
Format
Your function output is parsed into a response struct before being mapped to the HTTP response:
{
"statusCode": 200,
"headers": {
"Content-Type": "application/json",
"Custom-Header-1": "Custom Value"
},
"isBase64Encoded": false,
"body": "{\"message\":\"Hello FC!\"}"
}Mapping logic
Function Compute maps your function output to the HTTP response based on whether the output is valid JSON containing a statusCode field.
When the output is valid JSON with `statusCode`:
| Response struct field | HTTP response |
|---|---|
statusCode | Status code |
headers["Content-Type"] | Content-Type header (defaults to application/json if absent) |
body | Response body |
isBase64Encoded | Whether to Base64-decode the body before sending (defaults to false if absent) |
When the output is valid JSON without `statusCode`, or is not JSON:
Function Compute uses the following defaults:
| Field | Default value |
|---|---|
statusCode | 200 |
Content-Type | application/json |
body | Function output as-is |
isBase64Encoded | false |
Response mapping examples
The following examples show how function output flows through parsing to the final HTTP response.
Output for a string response
| Function output | Parsed response struct | HTTP response (client receives) |
|---|---|---|
Hello World! | {"statusCode":200,"body":"Hello World!","headers":{"content-type":"application/json"},"isBase64Encoded":false} | HTTP/1.1 200 OK<br>Content-Disposition: attachment<br>Content-Length: 12<br>Content-Type: application/json<br>X-Fc-Request-Id: 1-64f6d6e7-e01edb1cce58240ed59b59d9<br><br>Hello World! |
Output for a JSON response
| Function output | Parsed response struct | HTTP response (client receives) |
|---|---|---|
{"message": "Hello World!"} | {"statusCode":200,"body":"{\"message\": \"Hello World!\"}","headers":{"content-type":"application/json"},"isBase64Encoded":false} | HTTP/1.1 200 OK<br>Content-Disposition: attachment<br>Content-Length: 27<br>Content-Type: application/json<br>X-Fc-Request-Id: 1-64f6d867-7302fc1ac6338b6fd2adb782<br><br>{"message": "Hello World!"} |
Output for a custom response
| Function output | Parsed response struct | HTTP response (client receives) |
|---|---|---|
{"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 201 OK<br>Content-Type: application/json<br>My-Custom-Header: Custom Value<br>X-Fc-Request-Id: 1-64f6dcb3-e787580749d3ba13b047ce14<br><br>{"message": "Hello world!"} |
Base64 decoding
If your function returns valid JSON with isBase64Encoded set to true, Function Compute Base64-decodes the body before mapping it to the HTTP response body. If decoding fails, Function Compute returns the body value directly without reporting an error.
Response headers
Function Compute automatically adds the X-Fc-Request-Id header to every response. This header uniquely identifies the request and is useful for tracing logs and diagnosing errors.
Custom headers with the X-Fc- prefix are not supported. The following headers are reserved by Function Compute and are ignored if returned by your function:
connectioncontent-lengthdatekeep-aliveservercontent-disposition
Error handling
HTTP trigger invocations and direct API invocations handle function errors differently.
| Invocation type | Error behavior | HTTP status code |
|---|---|---|
| Direct API invocation | Error message returned in the response body | 200 |
| HTTP trigger (function URL) | Error message hidden; Internal Server Error returned | 502 |
When a function invoked via HTTP trigger encounters an error, the client receives a response like:
HTTP/1.1 502 Bad Gateway
Content-Disposition: attachment
Content-Type: application/json
X-Fc-Request-Id: 1-64f6df91-fe144d52e4fd27afe3d8dd6f
Content-Length: 21
Internal Server ErrorUse the X-Fc-Request-Id value to look up the full error details in your function's invocation logs.
Related topics
If you are writing function code for a built-in runtime, see the handler documentation for your language: