百度智能云

All Product Document

          API Gateway

          Backend Cloud Function Compute

          Introduction

          Cloud Function Compute (CFC) provides event-triggered, fully managed cloud computing capabilities. Due to its "serverless" feature, you only need to develop business code and upload it, without paying attention to and configuring server resources. Using the API gateway, you can directly use function computing as your backend, without paying attention to the backend service resources, while using the API gateway's traffic control, authentication, access control and other API management capabilities.

          Configuration Method

          Cloud Function Compute Configuration

          When the API gateway uses function computing as the backend, after receiving the http/https request from the client, the API gateway encapsulates the request information into an event format that matches the input of function computing. The event format (applicable to js/python) received by the function when the gateway calls CFC is as follows:

          { 
              "path": "HTTP Request Path",  
              "httpMethod": "HTTp request method", 
              "headers": {Request header},  
              "queryStringParameters": {query string parameter},  
              "pathParameters":   {Proxy path parameter},  
              "requestContext": {Request context},  
              "body": "Request Body",  
              "isBase64Encoded": "Whether the request body is Base64 encoded"  
          } 

          Note: When the API gateway is connected, the path in the event definition is slightly different from the event definition of the CFC http trigger. When the API gateway is connected, the resource field is included to indicate the request path defined in the API gateway, pathParameters indicates the matched path parameters, and path indicates the request path of the Cloud Function Compute itself.

          After the function calculates and processes the event, it needs to encapsulate the calculation result into a fixed format so that the API gateway can use this information to construct an http response. The return format (applicable to js/python) required by the function when the gateway calls CFC is as follows:

          { 
              "isBase64Encoded": true|false, 
              "statusCode": httpStatusCode, 
              "headers": { "headerName": "headerValue", ... }, 
              "body": "..." 
          } 

          In the case of JAVA, the input of the function is InputStream and the output is OutputStream. The format of InputStream converted to Json is consistent with the event format, and the returned result is encoded in UTF-8 and then written into OutputStream.

          Gateway Configuration

          For the basic API configuration method, please refer to API Open Guide.

          When configuring the API backend, select "Cloud Function Compute" as the backend service type, as shown in the figure below.

          After selecting the Cloud function compute, enter the BRN of the function in the backend service address, and other configurations are consistent with the configuration method of the normal backend, as shown in the figure below.

          The BRN of the function can be obtained through the following steps.

          Click "View Cloud Function Compute List".

          Click the specific function to enter the function details.

          After selecting the version, click "Copy” in the BRN column to copy the function BRN.

          Body Transfer Rules

          Gateway to Function

          The gateway needs to encapsulate the body in the client request into the "body" field of the input parameter event in json format. Different body formats have different processing methods. If the body from the client to the gateway is a key-value request, the body will be directly processed as json; for a request with a text body, the gateway sends a string to the backend; for the binary system sent to the backend, it is encoded by Base64 and then put into the body field of event in the form of string. The specific treatment is as follows:

          Process by Json
          • application/json
          • application/x-www-form-urlencoded
          Process by String
          • text/*
          • application/xml
          • application/rss+xml
          • application/problem+xml
          • application/atom+xml
          Binary Processing (after Base64Encode)
          • multipart/form-data
          • All other situations

          Function to Gateway

          The function returns to select whether to perform Base64 encoding according to business requirements, and indicates whether the body is encoded in isBase64Encoded. If the body returned by the function is Base64 encoded, the gateway will perform Base64 decoding before returning the body to the API user.

          Note: If the header returned to the gateway is not given a Conten-Type, the gateway will use application/json; charset=UTF-8 by default when returning to the user.

          Test Method

          Pay special attention to the following points:

          • Is the function call normal.
          • Is the content of path/query/header/body in the event sent to the function correct?
          • Is the request mapping sent to the function correct in case there is a parameter mapping.
          • Does the returned header meets expectations?
          • Do different content-types handle encoding correctly.
          • Encode the body returned by the function and confirm whether the body is decoded at the gateway level.
          Previous
          Cross-Origin Resource Sharing (CORS)
          Next
          Virtual Private Cloud (VPC) Open API