请教个Request Payload的问题
发布于 6 年前 作者 baiezone 12741 次浏览 最后一次编辑是 4 年前

做项目要更新redmine的状态,一直不成功,贴出代码,求各位大神指导!

var hex = ‘zEpLAwrPVTS47stg’;

(var contents = getPayload([
{name: '_method', value: 'put'},

{name: ‘issue[subject]’, value: ‘标题’}, {name: ‘done_ratio’, value: ‘55’}, {name: ‘commit’, value: ‘提交’} ], hex);

var options = { host: this.host, port: this.port, path: ‘/redmine/issues/2223’, method: ‘post’, headers: { ‘Content-Length’: + contents.length, ‘Content-Type’: ‘multipart/form-data; boundary=----WebKitFormBoundar’ + hex, ‘Cookie’: session } };

send(options, contents, function (header, data) { console.log(data); });)();

function send(option, data, callback) { var req = http.request(option, function(res) { var resData = ‘’;

res.setEncoding(‘utf8’);

res.on(‘data’, function (chunk) { resData += chunk; });

res.on(‘end’, function () { callback && callback(res.headers, resData); }); });

data && req.write(data); req.end(); }

function getPayload(array, hex) { var data = ‘’;

array.forEach(function (elm, index) { data += (’------WebKitFormBoundary’ + hex + ‘\r\n’ + ‘Content-Disposition: form-data; name="’ + elm.name + ‘"’ + ‘\r\n\r\n’ + elm.value + ‘\r\n’); });

data += (’------WebKitFormBoundary’ + hex);

return data; }

回到顶部