nodejs/express and binary data in POST nodejs/express and binary data in POST express express

nodejs/express and binary data in POST


It turned out to be an encoding issue. Everything seems to default to 'utf8', but in this case it needs to be set to 'binary'.

For example:

var buf = new Buffer(body.toString('binary'),'binary');

Equally as important, I needed to set nodejs multiparty parser's encoding to 'binary'. See: https://github.com/superjoe30/node-multiparty/issues/37


After struggling with this for way too long, I came up with this solution:

var express = require('express');var bodyParser = require('body-parser');var fs = require('fs');var path = require('path');app.use(bodyParser.raw({type: 'application/octet-stream', limit : '2mb'}))app.post('/nist-ws/rest/v1/nist/', function(req, res) {    var filePath = path.join(__dirname, 'nist_received', `/${Date.now()}.nist`)    fs.open(filePath, 'w', function(err, fd) {          fs.write(fd, req.body, 0, req.body.length, null, function(err) {            if (err) throw 'error writing file: ' + err;            fs.close(fd, function() {                console.log('wrote the file successfully');                res.status(200).end();            });        });    });});

bodyParser.raw fills req.body with a Buffer and the rest of the code is from: https://stackabuse.com/writing-to-files-in-node-js/