0

I'm trying to POST a raw body with restify. I have the receive side correct, when using POSTman I can send a raw zip file, and the file is correctly created on the server's file system. However, I'm struggling to write my test in mocha. Here is the code I have, any help would be greatly appreciated.

I've tried this approach.

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();

});

Yet the file size on the file system is always 0. I'm not using my readStream correctly, but I'm not sure how to correct it. Any help would be greatly appreciated.

Note that I want to stream the file, not load it in memory on transmit and receive, the file can potentially be too large for an in memory operation.

Thanks, Todd

tnine
  • 156
  • 9

2 Answers2

0

One thing is that you would need to specify a content-type of multi-part/form-data. However, it looks like restify doesn't support that content type, so you're probably out of luck using the restify client to post a file.

Community
  • 1
  • 1
HeadCode
  • 2,770
  • 1
  • 14
  • 26
  • 1
    I actually don't want to use multi-part/form-data. I'm not posting a form, I'm building an S3 style API where the payload body is RAW binary content. – tnine Feb 08 '16 at 18:49
0

To answer my own question, it doesn't appear to be possible to do this with the restify client. I also tried the request module, which claims to have this capability. However, when using their streaming examples, I always had a file size of 0 on the server. Below is a functional mocha integration test.

 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
tnine
  • 156
  • 9
  • This post might be of help: http://stackoverflow.com/questions/6158933/how-to-make-an-http-post-request-in-node-js You might also try the request module. – HeadCode Feb 09 '16 at 16:52