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?
Header | Description |
---|---|
(Optional) x-fc-base-path | If 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-status | This 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.
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 errorInvalidArgument
.- 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.
- You cannot customize a request header that starts with x-fc- or the following request headers:
- 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:attachmentNote 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 errorBadResponse
.- Header size: The total size of all keys and values in the headers cannot exceed 4 KB.
- You cannot customize a response header that starts with x-fc- or the following headers:
- 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
FAQ
- Must the listening port of a custom container runtime be the same as that of the HTTP server started in the custom container runtime?
- What do I do if the FunctionNotStarted error occurs when I invoke a third-party service in a service started in a custom container runtime?
- What do I do if a 502 error that contains the "Process exited unexpectedly before completing request" error message is returned?
- What do I do if a 404 error occurs when I use a browser or the cURL tool to access a function?
- What permissions must I grant to Function Compute service role if Alibaba Cloud public container images are not used?