使用node发送post请求
发布于 17小时前 作者 1043099804 58 次浏览 来自 分享

最近给公司写接口,写了个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();
1 回复
回到顶部