使用https.get()方法可以发送https的get请求,同时可以使用querystring模块将参数转换为查询字符串。

以下是一个示例代码:

const https = require('https');
const querystring = require('querystring');

const data = {
  name: '张三',
  age: 18,
  gender: 'male'
};

const options = {
  hostname: 'example.com',
  path: '/api?' + querystring.stringify(data),
  method: 'GET'
};

const req = https.request(options, res => {
  console.log(`statusCode: ${res.statusCode}`);

  res.on('data', d => {
    process.stdout.write(d);
  });
});

req.on('error', error => {
  console.error(error);
});

req.end();

在上面的示例中,我们定义了一个包含name、age和gender属性的数据对象。然后使用querystring模块的stringify方法将数据对象转换为查询字符串,并将其添加到请求路径的末尾。最后,使用https.request()方法发送请求。请求成功后,我们可以通过res.on()方法监听响应数据,并将其输出到控制台。

nodejs 使用https get请求传递参数

原文地址: https://www.cveoy.top/t/topic/bIsr 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录