nodejs 使用https请求设置参数
在使用Node.js进行HTTPS请求时,可以使用以下参数:
hostname: 要请求的服务器的主机名或IP地址。port: 要请求的服务器的端口号,默认为443。path: 要请求的路径。method: 请求方法,可以是GET、POST、PUT、DELETE等。headers: 请求头,需要以对象形式传入,例如:{ 'Content-Type': 'application/json' }。rejectUnauthorized: 是否验证服务器的证书,默认为true。如果要忽略证书验证,则可以设置为false。
以下是一个使用HTTPS请求的示例代码:
const https = require('https');
const options = {
hostname: 'www.example.com',
port: 443,
path: '/path/to/resource',
method: 'GET',
headers: {
'Content-Type': 'application/json'
},
rejectUnauthorized: false
};
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();
原文地址: https://www.cveoy.top/t/topic/bIrH 著作权归作者所有。请勿转载和采集!