Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Cloud development HTTP processing


May 22, 2021 Mini Program Cloud Development Advanced



In the small terminal we can use wx.request to interact with the third-party api service data, that cloud function in addition to directly to the small terminal to provide data, can we get data from the third-party server? T he answer is yes, and the use of HTTP in cloud functions to request access to third-party services can not be restricted by domain names, that is, do not need to be like a small terminal, to add domain names to the request legitimate domain name; However, it is important to note that cloud functions are deployed in the cloud, and some local area networks and other terminal communications business can only be carried out in small programs.

node popular HTTP libraries are more, such as got, superagent, request, axios, request-promise, fech and so on, it is recommended to use axios, axios is a promise-based HTTP library, can be used in browsers and Nodejs environment, the following will also take axios as an example.

First, get request

Using developer tools, create a cloud function, such as axios, and then add the dependency of the latest version of axios latest at package.json and install it with npm install:

  1. "dependencies": {
  2. "wx-server-sdk":"latest",
  3. "axios": "latest"
  4. }

Then enter the following code .js index, in the previous section, we called the API of the daily newspaper on the small terminal, and here is the API of the daily newspaper as an example:

  1. const cloud = require('wx-server-sdk')
  2. cloud.init({
  3. env: cloud.DYNAMIC_CURRENT_ENV,
  4. })
  5. const axios = require('axios')
  6. exports.main = async (event, context) => {
  7. const url = "https://news-at.zhihu.com/api/4/news/latest"
  8. try {
  9. const res = await axios.get(url)
  10. //const util = require('util')
  11. //console.log(util.inspect(res,{depth:null}))
  12. return res.data;
  13. } catch (e) {
  14. console.error(e);
  15. }
  16. }

Calling this cloud function on the small terminal, you can return the latest articles and popular articles obtained from the knowledge of the daily newspaper, the cloud function side to get the data of the know the daily newspaper does not need to add domain name verification, than the small terminal of wx.request convenient to save a lot of trouble.

Note that in the case above, we are not returning the entire response object, but the data in the response object. R eturning directly to the entire res object will report Converting circular structure to JSON and if you want to return the entire res, you can uncomment the code above. util.inspect(object,[showHidden],[depth],[colors]) is a method of converting any object to a string, typically for debugging and error output.

The above know link is the API, returns the data in the json format, so you can directly use axios.get(), axios can also be used for reptiles, crawling web pages, such as the following code is to crawl Baidu home page, and return to the home page of the content (that <title></title> the title of the page):

  1. const cloud = require('wx-server-sdk')
  2. cloud.init({
  3. env: cloud.DYNAMIC_CURRENT_ENV,
  4. })
  5. const axios = require('axios')
  6. exports.main = async (event, context) => {
  7. try {
  8. const res = await axios.get("https://baidu.com")
  9. const htmlString = res.data
  10. return htmlString.match(/<title[^>]*>([^<]+)<\/title>/)[1]
  11. } catch (e) {
  12. console.error(e);
  13. }
  14. }

If you want to use cloud functions to do reptile background, crawl web data, you can use third-party open source dependencies such as cheerio and puppeteer, there is not much to introduce here.

Second, post request

Combined with today's API in the history of aggregated data described earlier in the Network API, we can also initiate post requests at the cloud function side:

  1. const now = new Date(); //在云函数字符串时间时,注意要修改云函数的时区,方法在云函数实用工具库里有详细介绍
  2. const month = now.getMonth()+1 //月份需要+1
  3. const day = now.getDate()
  4. const key = "" //你的聚合KEY
  5. const url ="http://api.juheapi.com/japi/toh"
  6. const cloud = require('wx-server-sdk')
  7. cloud.init({
  8. env: cloud.DYNAMIC_CURRENT_ENV,
  9. })
  10. const axios = require('axios')
  11. exports.main = async (event, context) => {
  12. try {
  13. const res = await axios.post(url,{
  14. key:key,
  15. v:1.0,
  16. month:month,
  17. day:day
  18. })
  19. // const res = await axios.post(`url?key=${key}&v=1.0&month=${month}&day=${day}`)
  20. return res
  21. } catch (e) {
  22. console.error(e);
  23. }
  24. }

Third, use axios to download files

To download files using axios, you need to modify the responseType of axios from the default jason to stream, then upload the downloaded files to the cloud storage, or you can write the downloaded files to the temporary tmp folder of the cloud function for more complex operations.

  1. const cloud = require('wx-server-sdk')
  2. cloud.init({
  3. env: cloud.DYNAMIC_CURRENT_ENV,
  4. })
  5. const axios = require('axios')
  6. //const fs = require('fs');
  7. exports.main = async (event, context) => {
  8. try {
  9. const url = 'https://tcb-1251009918.cos.ap-guangzhou.myqcloud.com/weapp.jpg';
  10. const res = await axios.get(url,{
  11. responseType: 'stream'
  12. })
  13. const buffer = res.data
  14. //我们也还可以将下载好的图片保存在云函数的临时文件夹里
  15. // const fileStream = await fs.createReadStream('/tmp/axiosimg.jpg')
  16. return await cloud.uploadFile({
  17. cloudPath: 'axiosimg.jpg',
  18. fileContent: buffer,
  19. })
  20. } catch (e) {
  21. console.error(e);
  22. }
  23. }