使用node发送post请求
最近给公司写接口,写了个post请求的脚本,分享一下:)
var querystring = require(‘querystring’); var http = require(‘http’); var post_data = { ‘public_key’: this.publickey, ‘card[number]’: this.card.number, ‘card[card_exp_month]’: this.card.card_exp_month, ‘card[card_exp_year]’: this.card.card_exp_year, ‘card[card_cvv]’: this.card.card_cvv }; post_data = querystring.stringify(post_data); console.log(‘Post_data :’+post_data)
// set the request options
var post_options = {
host: 'example.com',
port: '80',
path: /path,
'method': 'Post',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
}
};
var post_req = http.request(post_options, function(res) {
res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log('Response: ' + chunk);
onetimetoken_data = JSON.parse(chunk);
callback(chunk);
});
});
post_req.write(post_data);
post_req.end();
3 回复
request,superagent
引号 有问题哟,,但做了调整之后,还是可以用的,感谢~
你只要用postman ,直接丢进去一个url请求,分分钟可以generate code 生成各种代码