HTTP handlers support only the custom container function for which CAPort is configured. This topic describes the structural characteristics, invocation descriptions, and limits of HTTP handlers in a custom container. This topic also provides examples on how to use an HTTP handler and answers to some commonly asked questions.

Background information

Function Compute forwards your requests, including methods, paths, queries, request headers, request bodies, and common headers generated by Function Compute to HTTP servers. You can smoothly migrate existing HTTP web applications. For more information, see HTTP functions.

Function invocation

You can send a request to invoke an HTTP handler by using cURL, Postman, or a browser. This method is similar to that of a web API operation. When you use a browser to access an HTTP trigger, a function may be downloaded in a forceful manner. For more information about how to resolve this issue, see When I use a web browser to access a function with an HTTP trigger, why do I need to download the response?

HeaderDescription
(Optional) x-fc-base-pathIf you do not specify a custom domain name, the value of x-fc-base-path is /2016-08-15/proxy/${servicename}/${functionname}/.
(Optional) x-fc-statusThis header behaves in a similar manner as the header in an event function. This is applicable to HTTP functions that are not migrated to Function Compute but created by calling a web API.
You can include the x-fc-status field in response headers to report to Function Compute whether the local function is successfully invoked.
  • If you do not specify a value for the x-fc-status field, Function Compute considers the invocation successful. If an error occurs during function execution, the system does not report the error to Function Compute. In this case, the business logic may not be affected, but the observability of Function Compute is affected. The following figure provides an example.image8hanshujisuanruntime
  • If you specify a value for the x-fc-status field, the system reports function invocation failures to Function Compute by using the x-fc-status field. If an error occurs during function invocation, the system records the error stack information in logs. The following figure provides an example.image9runtimefc
Note We recommend that you specify the StatusCode and x-fc-status fields in the HTTP response.

Usage limits

  • Only one HTTP trigger can be created for an HTTP function in each version or alias of a service. For more information, see Manage versions and Manage aliases.
  • HTTP request limits
    • You cannot customize a request header that starts with x-fc- or the following request headers:
      • connection
      • keep-alive
    • If a request exceeds one of the following limits, the system returns the status code 400 and the error 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 query parameters, cannot exceed 4 KB.
      • Body size: The total size of the body of a synchronous invocation request cannot exceed 32 MB. The total size of the body of an asynchronous invocation request cannot exceed 128 KB.
  • HTTP response limits
    • You cannot customize a response header that starts with x-fc- or the following headers:
      • connection
      • content-length
      • date
      • keep-alive
      • server
      • content-disposition:attachment
        Note For security purposes, the server forcefully adds the content-disposition: attachment field to the response header when the default domain name aliyuncs.com of Function Compute is used. When this field is added, the returned results are downloaded from the browser as attachments. To remove this limit, you must configure a custom domain name. For more information, see Configure a custom domain name.
    • If a response exceeds one of the following limits, the system returns the status code 502 and the error BadResponse.
      • Header size: The total size of all keys and values in the headers cannot exceed 4 KB.
  • Others

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

Sample code

In the following Node.js Express example, the GET and POST methods are routed to different handlers. You can map a path to a handler that you need.

'use strict';

const express = require('express');

// Constants
const PORT = 9000;
const HOST = '0.0.0.0';

// HTTP function get
const app = express();
// Parse request body as JSON
app.use(express.json())

app.get('/*', (req, res) => {
  console.log(req.body)
  res.send('Hello FunctionCompute, http GET');
});

app.post('/*', (req, res) => {
  console.log(req.body)
  res.send('Hello FunctionCompute, http POST');
});

app.listen(PORT, HOST);
console.log(`Running on http://${HOST}:${PORT}`);

Examples

Spring Boot

C++

.NET Core

FAQ