Javascript Node.js https.post 请求

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31606134/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 06:54:33  来源:igfitidea点击:

Node.js https.post request

javascriptnode.jshttppostrequest

提问by Nhor

I am using Node.js and I need to send a POST request containing specific data to an external server. I am doing the same thing with GET, but this is a lot easier since I don't have to include the additional data. So, my working GET request looks like:

我正在使用 Node.js,我需要将包含特定数据的 POST 请求发送到外部服务器。我正在用 GET 做同样的事情,但这要容易得多,因为我不必包含额外的数据。因此,我的工作 GET 请求如下所示:

var options = {
    hostname: 'internetofthings.ibmcloud.com',
    port: 443,
    path: '/api/devices',
    method: 'GET',
    auth: username + ':' + password
};
https.request(options, function(response) {
    ...
});

So I was wondering how to do the same thing with POST request, including data such as:

所以我想知道如何对 POST 请求做同样的事情,包括以下数据:

type: deviceType,
id: deviceId,
metadata: {
    address: {
        number: deviceNumber,
        street: deviceStreet
    }
}

Could anyone tell me how to include this data to the options above? Thanks in advance!

谁能告诉我如何将此数据包含在上述选项中?提前致谢!

回答by Dimitris P.

In the options object you include the request options as you did in the GET request and you create one more object containing the data you want in your POST's body. You stringify that using the querystring function (which you need to install by npm install querystring) and then you forward it by using the write() and end() methods of https.request().

在选项对象中,您像在 GET 请求中所做的那样包含请求选项,然后再创建一个包含您想要在 POST 正文中使用的数据的对象。您使用 querystring 函数(您需要通过 安装npm install querystring)对其进行字符串化,然后使用 https.request() 的 write() 和 end() 方法转发它。

It is important to note that you need two extra headers in your options object in order to make a successful post request. These are :

重要的是要注意,您的选项对象中需要两个额外的标头才能成功发布请求。这些是 :

'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': postBody.length

so you probably need to init your options object after querystring.stringify has returned. Otherwise you won't know the length of the stringified body data.

所以你可能需要在 querystring.stringify 返回后初始化你的选项对象。否则,您将不知道字符串化的正文数据的长度。

var querystring = require('querystring')
var https = require('https')


postData = {   //the POST request's body data
   type: deviceType,
   id: deviceId,
   metadata: {
      address: {
         number: deviceNumber,
         street: deviceStreet
      }
   }            
};

postBody = querystring.stringify(postData);
//init your options object after you call querystring.stringify because you  need
// the return string for the 'content length' header

options = {
   //your options which have to include the two headers
   headers : {
      'Content-Type': 'application/x-www-form-urlencoded',
      'Content-Length': postBody.length
   }
};


var postreq = https.request(options, function (res) {
        //Handle the response
});
postreq.write(postBody);
postreq.end();