Node / Javascript - 管道writeStream /文件发布请求

时间:2016-01-25 17:40:03

标签: javascript node.js post pipe

我有以下代码,它从一个测试var在远程服务器上创建一个文件(只是为了确保它有效),但是现在我需要上传一个文件,我不确定如何实际附加它对请求,这是代码:

var dataString = '@test.txt';
var options = {
    uri: 'https://site.zendesk.com/api/v2/uploads.json?filename=test.txt',
    method: 'POST',
    headers: {
        'Content-Type': 'application/binary',
         'Accept': 'application/json',
        Authorization: 'Basic bXVydGV6LmF....'
    },
    body: dataString 
    //this will create a test file with some text, but I need 
    //to upload a file on my machine instead
}

function callback(error, response, body) {
    if (!error && response.statusCode == 200) {
        console.log(body);
    }
    var x = {
        error: error,
        response: response,
        body: body
    }
    console.log(x);
}
    request(options, callback);

我正在考虑以下几点:

fs.createReadStream('text.txt').pipe({function?})

但不幸的是,我不确定如何完成这部分。

0 个答案:

没有答案