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

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

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

21条回答
  •  误落风尘
    2020-11-22 00:18

    Request-Promise Provides promise based response. http response codes other than 2xx will cause the promise to be rejected. This can be overwritten by setting options.simple = false

    var options = {
      method: 'POST',
      uri: 'http://api.posttestserver.com/post',
      body: {
      some: 'payload'
     },
      json: true // Automatically stringifies the body to JSON
    };
    
    rp(options)
    .then(function (parsedBody) {
        // POST succeeded...
    })
    .catch(function (err) {
        // POST failed...
    });
    

提交回复
热议问题