How is an HTTP POST request made in node.js?

后端 未结 21 2705
南方客
南方客 2020-11-21 23:54

How can I make an outbound HTTP POST request, with data, in node.js?

21条回答
  •  执笔经年
    2020-11-22 00:30

    I like the simplicity of superagent (https://github.com/visionmedia/superagent). Same API on both node and browser.

    ;(async function() {
      var response = await superagent.post('http://127.0.0.1:8125/', {age: 2})
      console.log(response)
    })
    
    

    There is also node-fetch (https://www.npmjs.com/package/node-fetch), which has an API that matches fetch from the browsers - however this requires manual query string encoding, does not automatically handle content types, or so any of the other work superagent does.

提交回复
热议问题