全部產品
Search
文件中心

Function Compute:HTTP觸發器調用函數

更新時間:Sep 15, 2026

HTTP觸發器提供了函數專用的HTTP和HTTPS地址,您可以直接通過HTTP觸發器提供的URL調用函數。本文將介紹內建運行時中使用HTTP觸發器調用函數的方法。關於自訂運行時HTTP觸發器調用函數的相關內容,請參見Web函數。

注意事項

在Function Compute3.0版中,自訂運行時和自訂鏡像運行環境的HTTP觸發器行為與Function Compute2.0版一致,但內建運行時函數的HTTP觸發器與Function Compute2.0版有較大的差異,詳情請參見調用函數流程。

調用函數流程

image

對於內建運行時函數,當用戶端調用函數URL時,Function Compute會將請求映射到事件對象event,再將event傳遞給函數。函數執行結束後,返回的響應將映射到一個HTTP響應,Function Compute會通過函數URL將HTTP響應發送回用戶端。

請求結構體

請求結構體格式

請求結構體格式如下:

{
    "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"
    }
}

請求結構體參數說明如下:

參數 說明 樣本
version 此事件的承載格式版本。目前版本為v1。 v1
rawPath 請求路徑。例如,如果請求URL為https://{url-id}.{region}.fcapp.run/example,則原始路徑值為/example。rawPath是經過URL編碼後,如果要擷取URL解碼後的path,請使用參數requestContext.http.path。 /example
body 請求的本文。如果請求的內容類型為二進位,則本文為Base64編碼。 Hello FC!
isBase64Encoded 如果本文為二進位,並且為Base64編碼,則為true,否則為false。 false
headers 該參數為要求標頭的列表。以索引值對的形式顯示,當一個鍵存在多個值時,值之間使用逗號分隔。
當使用HTTP觸發器調用內建運行時,Function Compute3.0將HTTP請求轉換成HTTP觸發器的Event格式,將HTTP請求Header的鍵進行正常化,Header的鍵的首字母變為大寫,詳情參見Header鍵的首字母為什麼變成了大寫?。
{"Header1": "value1", "Header2": "value1,value2"}
queryParameters 請求的查詢參數。例如,如果請求URL為https://{url-id}.{region}.fcapp.run/example?key1=value1,則queryParameters值是一個JSON對象,其鍵為key1,值為value1。當一個鍵存在多個值時,值之間會使用逗號分隔,比如{"key1": "value1", "key2": "value2,value3"}。 {
"parameter1": "value1",
"parameter2": "value1,value2"
}


requestContext 一個包含有關請求的附加資訊的對象,例如requestId、請求的時間以及通過授權的調用者身份。
requestContext.accountId 函數擁有者的阿里雲賬戶ID。 123456*********
requestContext.domainName 函數HTTP觸發器的網域名稱。 <http-trigger-id>.<region-id>.fcapp.run
requestContext.domainPrefix 函數HTTP觸發器的域首碼。 <http-trigger-id>
requestContext.http 包含有關HTTP請求的詳細資料。
requestContext.http.method 此請求中使用的HTTP方法。有效值包括GET、POST、PUT、HEAD、OPTIONS、PATCH和DELETE。 GET
requestContext.http.path 請求路徑。例如,如果請求URL為https://{url-id}.{region}.fcapp.run/example?name=Jane,則路徑值為/example。 /example
requestContext.http.protocol 請求的協議。 HTTP/1.1
requestContext.http.sourceIp 發出請求的即時TCP串連的源IP地址。該IP地址是直接建立串連的對端IP地址(RemoteAddr),即直接連接到伺服器的用戶端地址或最後一個代理的IP地址。
  • 如果請求沒有經過任何代理轉寄,RemoteAddr將是用戶端的IP地址。

  • 如果請求經過代理轉寄,RemoteAddr將是最後一個代理的IP地址。

說明:您可以通過HTTP要求標頭X-Forwarded-For擷取用戶端的原始IP地址,詳情請參見HTTP觸發器調用內建運行時函數時,如何擷取用戶端原始IP地址?。

11.11.XX.XX
requestContext.http.userAgent 使用者代理程式請求標題值。 PostmanRuntime/7.32.3
requestContext.requestId 調用請求的ID。可以使用此ID跟蹤與函數相關的調用日誌。 1-64f6cd87-*************
requestContext.time 請求的時間戳記。 2023-09-05T06:41:11Z
requestContext.timeEpoch 請求的時間戳記,用Unix時間表示。 1693896071895

Function ComputeHTTP請求映射邏輯

Function Compute會將HTTP請求映射成Event事件對象傳給請求處理常式(Handler),映射邏輯如下:

  • HTTP要求標頭映射為event結構中的headers。

  • HTTP請求參數映射為queryParameters。

  • HTTP請求的上下文資訊映射為requestContext。

  • POST請求的請求體映射為body。

Base64編碼機制

Function Compute在將HTTP請求映射到事件對象event時,會根據要求標頭中的Content-Type類型判斷是否對請求體進行Base64編碼:

  • 當要求標頭中的Content-Type表示文本類型時,不會對請求體進行Base64編碼,event結構中的isBase64Encoded設定為false。

  • 否則,會對請求體進行Base64編碼,isBase64Encoded設定為true。

以下為表示文本類型的Content-Type取值:

  • text/*

  • application/json

  • application/ld+json

  • application/xhtml+xml

  • application/xml

  • application/atom+xml

  • application/javascript

請求映射樣本

GET請求

HTTP請求 Event事件結構
GET /?parameter1=value1&parameter2=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" } } }
說明

您可以在命令列使用以下命令,發送上述HTTP請求。請將https://example.cn-hangzhou.fcapp.run替換為您的HTTP觸發器公網訪問地址。

curl -v "https://example.cn-hangzhou.fcapp.run?parameter1=value1&parameter2=value2"

POST請求

HTTP請求 Event事件結構
POST / HTTP/1.1
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" } } }
說明
  • 您可以在命令列使用以下命令,發送上述HTTP請求。請將https://example.cn-hangzhou.fcapp.run替換為您的HTTP觸發器公網訪問地址。

curl -v -H "Content-Type: application/json" -d '{"message": "Hello"}' "https://example.cn-hangzhou.fcapp.run"
  • 如果您需要發送Base64編碼的請求體,只需將要求標頭中的Content-Type設定為application/x-www-form-urlencoded。

響應結構體

響應結構體格式

函數的響應結構體格式如下。當您的請求處理常式返迴響應結構體時,Function Compute會解析響應並將其轉換為HTTP響應。

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

Function Compute響應解析邏輯

Function Compute會解析響應並構造HTTP Response返回給用戶端。

  • 如果您的函數返回有效JSON並且包含statusCode欄位,Function Compute解析邏輯如下:

    • statusCode:函數返回JSON中的statusCode值。

    • Content-Type:函數返回JSON中的Content-Type值。如果JSON中沒有Content-Type,Content-Type預設為application/json。

    • body:函數響應,函數返回JSON中的body值。

    • isBase64Encoded:函數返回JSON中的isBase64Encoded值,如果JSON中沒有isBase64Encoded,則預設為false。

  • 如果您的函數返回有效JSON但是沒有包含statusCode欄位,或者返回的不是有效JSON,Function Compute會做出以下假設,構造響應結構體。

    • statusCode:預設為200。

    • Content-Type:預設為application/json。

    • body:函數響應,即代碼中return的資料。

    • isBase64Encoded:預設為false。

Function Compute會將函數響應結構體映射成HTTP響應返回給用戶端,映射邏輯如下:

  • statusCode映射為HTTP響應的狀態代碼。

  • headers映射為HTTP回應標頭。

  • body映射為HTTP響應體,如果存在isBase64Encoded且為true,則先將body進行Base64解碼,再映射到HTTP響應體。

響應映射樣本

以下樣本介紹了函數的輸出如何映射到函數響應結構體,以及函數響應結構體如何映射到最終的HTTP響應。當用戶端調用函數HTTP觸發器時,就可以看到HTTP響應。

字串響應的輸出

函數輸出 解析函數輸出 HTTP響應(用戶端看到的內容)
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

Hello World!

JSON響應的輸出

函數輸出 解析函數輸出 HTTP響應(用戶端看到的內容)
{"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

{"message": "Hello World!"}

自訂響應的輸出

函數輸出 解析函數輸出 HTTP響應(用戶端看到的內容)
{ "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
Content-Type: application/json
My-Custom-Header: Custom Value
X-Fc-Request-Id: 1-64f6dcb3-e787580749d3ba13b047ce14

{"message": "Hello world!"}

Base64解碼機制

當函數輸出為有效JSON格式,且JSON中的isBase64Encoded欄位為true時,Function Compute會將JSON中的body欄位進行Base64解碼,再將解碼後的資料通過HTTP響應體返回給用戶端。

如果對body欄位解碼失敗,Function Compute不會報錯,而是直接將原始的body欄位的值返回給用戶端。

回應標頭(HTTP Response Header)

使用HTTP觸發器調用函數時,回應標頭中會包含Function Compute預設添加的回應標頭X-Fc-Request-Id,這是此次請求的唯一標識。除了X-Fc-Request-Id,Function Compute不會預設添加其他回應標頭。

您可以在代碼中返回自訂的回應標頭,但不支援X-Fc-開頭的回應標頭和以下Function Compute保留的回應標頭:

  • connection

  • content-length

  • date

  • keep-alive

  • server

  • content-disposition

如果您在回應標頭中設定了這些保留字,Function Compute會直接忽略您設定的回應標頭。

錯誤處理

當遇到函數錯誤時,通過API調用會返回具體的錯誤資訊,HTTP返回碼為200。比如Python的ModuleNotFound錯誤的響應如下:

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

但使用HTTP 觸發器調用時,Function Compute會隱藏這些錯誤資訊,直接返回Internal Server Error,HTTP返回碼為502。HTTP響應樣本如下:

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 Error

此時,您可以通過請求返回的X-Fc-Request-Id在日誌中尋找具體的報錯資訊。

相關文檔

在Function Compute3.0內建運行時中編寫函數代碼時,請參見以下運行時的請求處理常式(Handler)文檔。