Advanced scenario 4: Call an HTTP service

更新时间:
复制 MD 格式

Function Compute provides the ctx.httpclient object to make HTTP and HTTPS requests to any web service. No additional dependencies are required.

The following example shows how to request a web page.

module.exports = async ctx => {
  const res = await ctx.httpclient.request('https://www.example.com/x/xxx.htm');
  return {
    html: res.status === 200 ? res.data : '',
  };
};
                

The ctx.httpclient object is an instance of urllib.