请求NODEjs到https网站和encodeurl
答案 0 :(得分:0)
简单。使用request模块
var request = require('request');
var formData = {
// Pass a simple key-value pair
my_field: 'my_value'
};
request.post({url:'https://service.com/post', formData: formData}, function(err, httpResponse, body) {
if (err) {
return console.error('post failed:', err);
}
console.log('post successful! Server responded with:', body);
});
如果您想使用基本的https模块。
var https = require('https');
var options = {
host:'www.example.com',
post: 80,
path:'/post',
method:'POST'
};
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.write('{"name":"biblo","occupation":"Burglar"}');
req.end();
还有很多类似的模块。 axios因其承诺回应而闻名。