【问题标题】:POSTing RAW body with restify client使用 restify 客户端发布 RAW 正文
【发布时间】:2016-02-09 02:47:33
【问题描述】:

我正在尝试用 restify 发布一个原始的身体。我的接收端是正确的,当使用 POSTman 时,我可以发送一个原始 zip 文件,并且该文件是在服务器的文件系统上正确创建的。但是,我正在努力用 mocha 编写我的测试。这是我的代码,任何帮助将不胜感激。

我已经尝试过这种方法。

const should = require('should');
const restify = require('restify');
const fs = require('fs');

const port = 8080;
const url = 'http://localhost:' + port;

const client = restify.createJsonClient({
    url: url,
    version: '~1.0'
});



const testPath = 'test/assets/test.zip';
fs.existsSync(testPath).should.equal(true);


const readStream = fs.createReadStream(testPath);

client.post('/v1/deploy', readStream, function(err, req, res, data) {
        if (err) {
            throw new Error(err);
        }

        should(res).not.null();
        should(res.statusCode).not.null();
        should(res.statusCode).not.undefined();
        res.statusCode.should.equal(200);


        should(data).not.null();
        should(data.endpoint).not.undefined();
        data.endpoint.should.equal('http://endpointyouhit:8080');


        done();

});

但文件系统上的文件大小始终为 0。我没有正确使用我的 readStream,但我不确定如何更正它。任何帮助将不胜感激。

请注意,我想流式传输文件,而不是在发送和接收时将其加载到内存中,文件可能太大而无法进行内存操作。

谢谢, 托德

【问题讨论】:

    标签: node.js restify


    【解决方案1】:

    有一件事是您需要指定多部分/表单数据的内容类型。但是,它看起来像 restify doesn't support that content type,所以您可能不走运使用 restify 客户端发布文件。

    【讨论】:

    • 我实际上不想使用多部分/表单数据。我没有发布表单,我正在构建一个 S3 风格的 API,其中有效负载主体是 RAW 二进制内容。
    【解决方案2】:

    要回答我自己的问题,restify 客户端似乎无法做到这一点。我还尝试了 request 模块,它声称具有此功能。但是,在使用他们的流媒体示例时,我在服务器上的文件大小始终为 0。下面是一个功能性 mocha 集成测试。

     const testPath = 'test/assets/test.zip';
            fs.existsSync(testPath).should.equal(true);
    
    
            const readStream = fs.createReadStream(testPath);
    
    
            var options = {
                host: 'localhost'
                , port: port
                , path: '/v1/deploy/testvalue'
                , method: 'PUT'
            };
    
            var req = http.request(options, function (res) {
                //this feels a bit backwards, but these are evaluated AFTER the read stream has closed
    
                var buffer = '';
    
                //pipe body to a buffer
                res.on('data', function(data){
                    buffer+= data;
                });
    
                res.on('end', function () {
    
                    should(res).not.null();
                    should(res.statusCode).not.null();
                    should(res.statusCode).not.undefined();
                    res.statusCode.should.equal(200);
    
    
                    const json = JSON.parse(buffer);
    
                    should(json).not.null();
                    should(json.endpoint).not.undefined();
                    json.endpoint.should.equal('http://endpointyouhit:8080');
    
    
                    done();
                });
    
            });
    
            req.on('error', function (err) {
                if (err) {
                    throw new Error(err);
                }
    
            });
    
            //pipe the readstream into the request
            readStream.pipe(req);
    
    
            /**
             * Close the request on the close of the read stream
             */
            readStream.on('close', function () {
                req.end();
                console.log('I finished.');
            });
    
            //note that if we end up with larger files, we may want to support the continue, much as S3 does
            //https://nodejs.org/api/http.html#http_event_continue
    

    【讨论】:

    猜你喜欢
    • 2013-09-07
    • 1970-01-01
    • 2012-10-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-24
    • 2020-06-04
    • 2013-09-12
    • 2023-04-08
    相关资源
    最近更新 更多