Marco24690
Marco24690

Reputation: 355

Chai POST send binary file

I need to test a file upload, without using multi part form data.

it('Create node', () => {
    return chai.request(server)
        .post('/api/sensornodes')
        .set('Content-Type', 'application/json')
        .send(fs.readFileSync('test/manifest/sensor_nodes.json'))
        .then((res) => {
            expect(res.status).to.eql(204);
        });
});

This test call the below endpoint, where I'm using the body-parser to parse the request

public create(req: Request, res: Response, next: NextFunction) {
    console.log("req body", req.body)
}

But when I execute the test the req.body is a an object like this, not as I'm expecting

{ 
    type: 'Buffer',
    data:
       [ 123,
         10,
         32,
         32,
         32,
         ...
       ]
}

When I call that endpoint through Postman, everything it's ok and in the req.body I have the content of the .json

Where I am wrong?

Upvotes: 1

Views: 1089

Answers (1)

Marco24690
Marco24690

Reputation: 355

I was missing the encode type

I've fixed it putting

.send(fs.readFileSync('test/manifest/sensor_nodes.json', 'utf8'))

Upvotes: 2

Related Questions