This topic describes how to determine and invoke HTTP functions in a custom runtime and provides sample code and limits.

Background information

Function Compute forwards your requests, including the method, path, query, request headers, request body, and common headers generated by Function Compute to the HTTP server. HTTP functions allow you to migrate existing HTTP web applications.

Determine the type of the invoked function

For a custom runtime, you can determine whether a request is used to invoke an HTTP function or an event function based on the x-fc-control-path header.

Note Generally, you do not need to take note of the x-fc-control-path header.
  • /invoke: The request is used to invoke an event function. /invoke indicates that the request is used to invoke an event function.
  • /initialize: The request is automatically initiated by Function Compute to invoke an initializer function when an execution environment is created for the first time. In the lifecycle of a container, the initializer function is invoked only once, which is similar to the class constructor.
  • /http-invoke: The request is used to invoke an HTTP function. /http-invoke indicates that the request is used to invoke an HTTP function. Function Compute adds common headers to your request that includes the HTTP method, request path, request body, query parameters, and request headers and forwards the request to the custom runtime. The custom runtime returns the response headers and body to the client.

Invocations

The invocation of an HTTP function is like the invocation of a web API. You can initiate an invocation request by using cURL, Postman, or a browser. For more information about the solution to the case where the relevant function is downloaded in a forced manner when you use a browse to access an HTTP trigger, see Solution.

Notice If the initializer parameter is not specified when a function is created, /initialize does not need to be implemented. In this case, even if /initialize is implemented by the HTTP server, the /initialize logic in the code cannot be invoked or executed.
Path Request Expected response
(Optional) POST /initialize

Request body: none.

Request header: common request headers. For more information, see Common request headers in Function Compute.

Response body: the return value of the function initializer.
StatusCode
  • 200: succeeded.
  • 404: failed.
Header Description
(Optional) x-fc-base-path If a custom domain name is not specified, the x-fc-base-path value is /2016-08-15/proxy/${servicename}/${functionname}/.
(Optional) x-fc-status If your HTTP function is not used for a migration but as a new web API, it is similar to an event function. The x-fc-status field can be used to report to Function Compute whether the function invocation is successful. For more information, see Overview.

Limits

  • After an HTTP trigger is set for a function, other types of triggers cannot be set for this function.
  • Only one HTTP trigger can be created for each function.
  • If version management is enabled for a function, only one HTTP trigger can be created for each version or alias of the function. In other words, one HTTP trigger can be created for one version or one alias of a function. For more information about versions and aliases, see Introduction to versions.
  • The following fields in a request header and request headers that start with x-fc- cannot be customized:
    • accept-encoding
    • connection
    • keep-alive
    • proxy-authorization
    • te
    • trailer
    • transfer-encoding
  • The following fields in a response header and response headers that start with x-fc- cannot be customized:
    • connection
    • content-length
    • content-encoding
    • date
    • keep-alive
    • proxy-authenticate
    • server
    • trailer
    • transfer-encoding
    • upgrade
    • content-disposition:attachment
      Note For security, when the default aliyuncs.com domain name of Function Compute is used, the server forcibly adds the content-disposition: attachment field to the response header. This field is used to download the returned result in the browser as an attachment. To remove this limit, you must set a custom domain name.
  • Limits on HTTP requests

    If a request exceeds one of the following limits, the system returns status code 400 and error code InvalidArgument.

    • Header size: The total size of all keys and values in the headers cannot exceed 4 KB.
    • Path size: The total size of the path, including all of the query parameters, cannot exceed 4 KB.
    • Body size: The total size of the HTTP request body cannot exceed 6 MB.
  • Limits on HTTP responses

    If a response exceeds one of the following limits, the system returns status code 502 and error code BadResponse.

    • Header size: The total size of all keys and values in the headers cannot exceed 4 KB.
  • Other instructions

    You can map different HTTP paths for HTTP functions by binding a custom domain name. You can also use API Gateway, set the backend service type to HTTP, and configure the HTTP function path as the backend service address to implement similar features. For more information, see Use Function Compute as the backend service of an API operation.