使用 chai 在 TypeScript 中处理 BLOB(mime 类型八位字节流)的正确方法是什么?

What is the right way to process BLOB (mime type octet stream) in TypeScript with chai?

我在正在进行的项目中获得了我在 TypeScript 方面的第一次经验,此时我正在尝试弄清楚什么是处理从服务器传递的 blob(csv 文件)的正确方法。

客户端团队使用 TypeScript、chai、chai http。

服务器 returns 200 代码为空 {} body。

这里是代码:

chai.usees(chaiHttp);

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

我写这个概念是为了避免 copy/paste 代码来自项目(我不确定这个项目的 NDA 范围)。我觉得理解意思就够了。

除了 headers 之外,我还需要其他任何东西来获取我的 csv 文件吗?我希望它在这里作为 base64 字符串。

p.s。 这是一个持续的开发,所以问题可能出在服务器上或缺乏特定的业务知识,但我必须确保客户端没有问题。

线索是here

确实,在带有 chai 的 TypeScript 中,我们必须做一些额外的工作来处理二进制类型,并且需要特殊的 headers。

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'));
    });
};

(代码属于作者link以上)

服务器响应 200 代码和 body 中的 {} 是正确的。