使用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();