All Products
Search
Document Center

Function Compute:HTTP handlers

Last Updated:Oct 18, 2024

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

The method that you can use to invoke an HTTP handler is similar to that of a web API operation. You can send a request to invoke an HTTP handler by using cURL, Postman, or a browser. 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?

Header

Description

(Optional) x-fc-base-path

If you do not specify a custom domain name, /2016-08-15/proxy/${servicename}/${functionname}/ is used as the value of x-fc-base-path.

(Optional) x-fc-status

This header behaves in a similar manner to a 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 operation.

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 notes

  • 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.

  • Limits on HTTP requests

    • Request headers do not support custom fields that start with x-fc- or the following fields:

      • connection

      • keep-alive

    • The system returns the 400 status code and InvalidArgument error code if a request exceeds one of the following limits:

      • Headers size: The total size of all keys and values in headers cannot exceed 8 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

    • Response headers do not support custom fields that start with x-fc- or the following fields:

      • connection

      • content-length

      • date

      • keep-alive

      • server

      • content-disposition:attachment

        Note

        For security reasons, if you use the default domain name aliyuncs.com of Function Compute, the server forcibly adds the content-disposition: attachment field to response headers. This field makes the returned results to be downloaded as an attachment in a browser. To remove this limit, you must specify 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 status code 502 and error BadResponse.

      • Headers size: The total size of all keys and values in headers cannot exceed 8 KB.

  • Others

    You can bind custom domain names 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

FAQ