HTTP handler

更新时间:
复制 MD 格式

HTTP handlers let you run existing HTTP web applications in Function Compute without rewriting them. Function Compute supports HTTP handlers only for custom container functions with CAPort configured.

How it works

When a request arrives, Function Compute forwards it to the HTTP server running in your container. The forwarded request preserves all original attributes: method, path, query parameters, request headers, request body, and common headers generated by Function Compute.

Your container's HTTP server processes the request and returns a response. Function Compute then forwards that response to the caller.

Invoke an HTTP handler

Send requests to an HTTP handler the same way you call any web API — using cURL, Postman, or a browser.

When accessing an HTTP trigger from a browser, the response may be downloaded as a file instead of rendered. For details, see When I use a web browser to access a function with an HTTP trigger, why do I need to download the response?

The following Node.js Express example routes GET and POST requests to separate handlers. Map any path to the handler you need.

'use strict';

const express = require('express');

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

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}`);

For additional examples in other runtimes, see Examples.

Request and response headers

Special request headers

Function Compute adds the following headers to requests forwarded to your container.

HeaderRequiredDescription
x-fc-base-pathOptionalThe base path prepended to the request path. If no custom domain name is configured, the value is /2016-08-15/proxy/${servicename}/${functionname}/.
x-fc-statusOptionalApplicable to HTTP functions created by calling a web API operation that are not migrated to Function Compute. Include this field in the response header to report invocation status back to Function Compute.

How `x-fc-status` affects observability:

x-fc-status in responseBehavior
Not setFunction Compute treats the invocation as successful. Errors during function execution are not reported to Function Compute — business logic is unaffected, but FC observability is impacted. image8hanshujisuanruntime
SetFunction invocation failures are reported to Function Compute via x-fc-status. Error stack information is recorded in logs. image9runtimefc

Set both StatusCode and x-fc-status in your HTTP response to maintain full observability.

Restricted headers

The following headers cannot be used in requests or responses.

Requests — the following headers are not supported as custom fields:

  • Any field starting with x-fc-

  • connection

  • keep-alive

Responses — the following headers are not supported as custom fields:

  • Any field starting with x-fc-

  • connection

  • content-length

  • date

  • keep-alive

  • server

  • content-disposition:attachment

When using the default Function Compute domain (aliyuncs.com), the server automatically adds content-disposition: attachment to all responses, causing browsers to download them as files. To remove this restriction, configure a custom domain name.

Limits

HTTP trigger limit

Each version or alias of an HTTP function supports only one HTTP trigger. For details, see Manage versions and Manage aliases.

Request and response size limits

CategoryLimitValueError on exceeded
RequestHeader size (all keys + values)8 KB400 InvalidArgument
RequestPath size (including all query parameters)4 KB400 InvalidArgument
RequestBody size — synchronous invocation32 MB400 InvalidArgument
RequestBody size — asynchronous invocation128 KB400 InvalidArgument
ResponseHeader size (all keys + values)8 KB502 BadResponse

Custom domain names and API Gateway

Bind custom domain names to your HTTP functions to map different URL paths to different functions. See Configure a custom domain name.

Alternatively, use API Gateway with its backend service type set to HTTP and the HTTP function path as the backend service address. See Use Function Compute as the backend service of an API operation.

Examples

FAQ