0

I'm obtain my first experience in TypeScript on ongoing project and at this moment try to figure out what is the right way to process blobs (csv file) passed from server.

On client side team uses TypeScript, chai, chai http.

Server returns 200 code with empty {} body.

Here the code:

chai.usees(chaiHttp);

const response = chai.request(endpoint/csv)
  .set('Content-Type', 'application/octet-stream');
response.status // 200
response.body   // {}

I write the concept to avoid copy/paste code from the project (I'am not sure about scope of NDA on this project). I think it is enough to understand the meaning.

Do I need anything else besides headers to obtain my csv file here? I would expect it here as base64 string.

p.s. This is an ongoing development, so issue might be on the server or some lack of specific business knowledge, but I must sure there is no issue on the client side.

Gleichmut
  • 5,953
  • 5
  • 24
  • 32

1 Answers1

0

The clue was here

Indeed, in TypeScript with chai we have to do some extra work to process binary type and special headers are required.

chai.request(url)
    .get('/api/exercise/1?token=' + token)
    .buffer()
    .parse(binaryParser)
    .end(function(err, res) {
        if (err) { done(err); }
        res.should.have.status(200);

        // Check the headers for type and size
        res.should.have.header('content-type');
        res.header['content-type'].should.be.equal('application/pdf');
        res.should.have.header('content-length');
        const size = fs.statSync(filepath).size.toString();
        res.header['content-length'].should.be.equal(size);

        // verify checksum                
        expect(md5(res.body)).to.equal('fa7d7e650b2cec68f302b31ba28235d8');              
    });

const binaryParser = function (res, cb) {
    res.setEncoding('binary');
    res.data = '';
    res.on("data", function (chunk) {
        res.data += chunk;
    });
    res.on('end', function () {
        cb(null, new Buffer(res.data, 'binary'));
    });
};

(code belongs to author by link above)

The server response was correct 200 code and {} in the body.

Gleichmut
  • 5,953
  • 5
  • 24
  • 32