文档

云函数支持固定出网IP

背景信息

云函数的出网IP是变化的,但一些三方系统,要求配置固定IP白名单,例如微信公众号开发get-access-token接口,此时需要提供在云函数中固定出网IP地址的能力。

实现原理

云函数入参ctx提供对象httpProxyClient,通过该对象发起的HTTP请求将会走代理服务器网络出口从而获得固定出网IP的能力。代理服务器当前限制仅支持weixin.qq.com。若开发者有其他域名代理需求,可提工单加白。

代理服务器IP列表如下,请将以下IP全部加入白名单配置:

47.92.132.2
47.92.152.34
47.92.87.58
47.92.207.183
8.142.185.204

使用方法

发送Get请求

用法

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

示例

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

// result 格式为字符串,需要做一次JSON解析
return JSON.parse(result)

输出如下:

{
    "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
}
重要

body的格式需要根据Content-Type判断,如果是application/json,则body为JSON格式,否则一律返回字符串格式。

POST 表单数据

用法

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

示例

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

POST JSON 数据

用法

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

示例

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

POST 通用数据

用法

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

示例

ctx.httpProxyClient.post(    
    'https://www.example.com/search',
    'abcdefg',
    {
      "Content-Type": "text/plain"
    }
)
重要
  1. 请求默认超时时间为30000 毫秒。

  2. 暂不支持文件流类型以及multipart格式。

  • 本页导读 (0)
文档反馈