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

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

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

21条回答
  •  渐次进展
    2020-11-22 00:09

    This is the simplest way I use to make request: using 'request' module.

    Command to install 'request' module :

    $ npm install request
    

    Example code:

    var request = require('request')
    
    var options = {
      method: 'post',
      body: postData, // Javascript object
      json: true, // Use,If you are sending JSON data
      url: url,
      headers: {
        // Specify headers, If any
      }
    }
    
    request(options, function (err, res, body) {
      if (err) {
        console.log('Error :', err)
        return
      }
      console.log(' Body :', body)
    
    });
    

    You can also use Node.js's built-in 'http' module to make request.

提交回复
热议问题