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

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

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

21条回答
  •  南笙
    南笙 (楼主)
    2020-11-22 00:26

    By using request dependency.

    Simple solution :

     import request from 'request'
     var data = {
            "host":"127.1.1.1",
            "port":9008
        }
    
    request.post( baseUrl + '/peers/connect',
            {
                json: data,  // your payload data placed here
                headers: {
                    'X-Api-Key': 'dajzmj6gfuzmbfnhamsbuxivc', // if authentication needed
                    'Content-Type': 'application/json' 
                }
            }, function (error, response, body) {
                if (error) {
                    callback(error, null)
                } else {
                    callback(error, response.body)
                }
            });
    

提交回复
热议问题