beecloud resrful api test(nodejs)

直接上代码


/**
 * Created by wyh on 2015/10/8.
 * 参数说明:https://beecloud.cn/doc/
 */
var https  = require('https');
var crypto = require('crypto');
var moment = require('moment');
var uuid   = require('node-uuid');

var app_id     = 'yourAppId';
var timestamp  = moment().format('x');
var app_secret = 'YourAppSecret';
var app_sign   = crypto.createHash('md5').update(app_id + timestamp + app_secret).digest('hex');
var channel    = 'ALI_WEB';
var total_fee  = 1;
var bill_no    = uuid.v4().split('-').join('');
console.log(bill_no);

var title       = '图时代充值测试';
var return_url  = 'visys.cn';
var show_url    = 'http://www.visys.cn';
var qr_pay_mode = '0';

var postObj = {
    app_id     : app_id,
    timestamp  : parseInt(timestamp),
    app_sign   : app_sign,
    channel    : channel,
    total_fee  : total_fee,
    bill_no    : bill_no,
    title      : title,
    return_url : return_url,
    show_url   : show_url,
    qr_pay_mode: qr_pay_mode
};

var postData = JSON.stringify(postObj);
console.log('postData', postData);

var options = {
    host   : 'apibj.beecloud.cn',
    port   : 443,
    path   : '/1/rest/bill',
    method : 'POST',
    headers: {
        'Content-Type': 'application/json'
    }
};

var req = https.request(options, function (res) {
    console.log('STATUS: ' + res.statusCode);
    console.log('HEADERS: ' + JSON.stringify(res.headers));

    var chunkAll = '';
    res.on('data', function (chunk) {
        chunkAll += chunk;
    });
    res.on('end', function () {
        if(!chunkAll) return;
        try{
            var obj = JSON.parse(chunkAll);
            console.log('url', obj.url);
            console.log('chunkAll' + chunkAll);
        }
        catch(e){
            console.log('err', e);
        }
    });
});

req.write(postData);
req.end();

req.on('error', function (e) {
    console.error(e);
});

附:

header不能添加content-length

原文地址:https://www.cnblogs.com/yanhuiw/p/4863363.html