Use static egress IPs with Function Compute

更新时间:
复制 MD 格式

Background information

Function Compute uses dynamic egress IP addresses. Some third-party systems require you to configure a whitelist of static IP addresses. For example, you need a static egress IP address for your function when you develop for a WeChat Official Account and call the `get-access-token` API.

How it works

The Function Compute input parameter `ctx` provides an `httpProxyClient` object. HTTP requests sent using this object are routed through a proxy server. This provides a static egress IP address. The proxy server currently supports only weixin.qq.com. To use other domain names, submit a ticket to add them to the whitelist.

The proxy server IP addresses are listed below. Add all of these IP addresses to your whitelist.

47.92.132.2
47.92.152.34
47.92.87.58
47.92.207.183
8.142.185.204

Usage

Send a Get request

Method

ctx.httpProxyClient.get(url: String, params: Object?, headers: Object?)

Example

const result = await ctx.httpProxyClient.get('https://api.weixin.qq.com/cgi-bin/token',
    {
        grant_type: 'client_credential', 
        appid: 'xxxx',
        secret: 'xxxx'
    }
)

// The result is a string. You need to parse it as JSON.
return JSON.parse(result)

The output is as follows:

{
    "headers": {
        "Connection": "keep-alive",
        "Content-Type": "application/json; encoding=utf-8",
        "Date": "Sat, 08 Oct 2022 11:52:20 GMT",
        "Content-Length": "194"
    },
    "body": {
        "access_token": "61_7kK2pb8ijdkz-********",
        "expires_in": 7200
    },
    "statusCode": "OK",
    "statusCodeValue": 200
}
Important

The format of the `body` depends on the `Content-Type` header. If the `Content-Type` is `application/json`, the body is JSON-formatted. Otherwise, the body is returned as a string.

POST form data

Usage

ctx.httpProxyClient.postForm(url: String, data: Object?, headers: Object?)

Example

ctx.httpProxyClient.postForm(    
    'https://www.example.com/search', 
    {
        q: 'nodejs', 
        cat: '1001'
    }
)

POST JSON-formatted data

Method

ctx.httpProxyClient.postJson(url: String, json: Object?, headers: Object?)

Example

ctx.httpProxyClient.postForm(    
    'https://www.example.com/search', 
    {
        q: 'nodejs', 
        cat: '1001'
    }
)

POST generic data

Usage

ctx.httpProxyClient.post(url: String, text: String?, headers: Object?)

Example

ctx.httpProxyClient.post(    
    'https://www.example.com/search',
    'abcdefg',
    {
      "Content-Type": "text/plain"
    }
)
Important
  1. The default request timeout is 30,000 ms.

  2. File streams and the `multipart` format are not supported.