nodeJS-发出HTTPS请求,发送JSON数据

nodeJS-发出HTTPS请求,发送JSON数据,json,node.js,https,Json,Node.js,Https,我想从一个nodeJS服务器向另一个发送HTTPS帖子。我有一些JSON数据要随此请求一起发送(由html表单填充) 我该怎么做?我知道https.request(),但似乎没有将JSON作为查询的一部分的选项。根据我的研究,似乎可以使用HTTP请求,但不能使用HTTPS请求。我怎样才能解决这个问题 const pug = require('pug'); var cloudinary = require('cloudinary'); var express = require('express'

我想从一个nodeJS服务器向另一个发送HTTPS帖子。我有一些JSON数据要随此请求一起发送(由html表单填充)

我该怎么做?我知道https.request(),但似乎没有将JSON作为查询的一部分的选项。根据我的研究,似乎可以使用HTTP请求,但不能使用HTTPS请求。我怎样才能解决这个问题

const pug = require('pug');
var cloudinary = require('cloudinary');
var express = require('express');
var multer = require('multer');
var upload = multer({ dest: 'uploads/' });
var request = require('request');
var bodyParser = require('body-parser');

var options = {
 hostname: 'ec2-54-202-139-197.us-west-2.compute.amazonaws.com',
 port: 443,
 path: '/',
 method: 'GET'
};

var app = express();
var parser = bodyParser.raw();
app.use(parser);

app.set('view engine', 'pug');

app.get('/', upload.single('avatar'), function(req, res) {
 return res.render('index.pug');
});

app.get('/makeRequest*', function(req, res) {
 query = req['query'];
 /*
 Here, I would like to send the contents of the query variable as JSON to the server specified in options.
 */
});

您可以使用本机https节点模块通过POST http请求发送JSON数据,如下所示

http.request()中的所有选项都有效

因此,以http.request()为例,您可以执行以下操作:

var postData = querystring.stringify({
  'msg' : 'Hello World!'
});

var options = {
  hostname: 'www.google.com',
  port: 80,
  path: '/upload',
  method: 'POST',
  headers: {
    'Content-Type': 'application/x-www-form-urlencoded',
    'Content-Length': Buffer.byteLength(postData)
 }
};

var req = https.request(options, (res) => {
  console.log(`STATUS: ${res.statusCode}`);
  console.log(`HEADERS: ${JSON.stringify(res.headers)}`);
  res.setEncoding('utf8');
  res.on('data', (chunk) => {
    console.log(`BODY: ${chunk}`);
  });
  res.on('end', () => {
    console.log('No more data in response.');
  });
});

req.on('error', (e) => {
  console.log(`problem with request: ${e.message}`);
});

// write data to request body
req.write(postData);
req.end();

您应该将
postData
编辑为所需的JSON对象

我相信下面就是您想要的。使用图书馆。有关我的建议,请参见代码中的注释

...

var options = {
  hostname: 'ec2-54-202-139-197.us-west-2.compute.amazonaws.com',
  port: 443,
  path: '/',
  method: 'POST',
  json: true
};

...

//making a post request and sending up your query is better then putting it in the query string
app.post('/makeRequest', function(req, res) {
  var query = req.body['query'];

  //NOTE, you cannot use a GET request to send JSON. You'll need to use a POST request. 
  //(you may need to make changes on your other servers)
  options.body = { payload: query };
  request(options, function(err, response, body) {
    if (err) {
      //Handle error
      return;
    }

    if (response.statusCode == 200) {
      console.log('contents received');
    }

  });
});

正如matt提到的,你需要使用

要发送JSON对象而不是JSON.Stringify,以便在服务器上使用以下命令接收它:

app.post('/makeRequest', function(req, res) {
console.log (req.body.param1);
}
使用以下代码:

var request = require("request");

request({
        'url':"http://www.url.com",
         method: "POST",
        json: true, 
        body: {'param1':'any value'}
    }, function (error, resp, body) {
        console.log ("check response.");
    });

这不起作用,服务器不接收postData。我已经尝试过了,它确实起作用,请记住,在您将处理请求的服务器中,如果是您保存了我的一天的节点服务器,则数据将包含在
req.body
中。我忘了包括
req.write(postData)。啊,啊,一瞬间