KMC
KMC

Reputation: 1742

Strange behavior from Busboy

I'm having strange problem with Busboy. I'm uploading file from powershell by using Invoke-RestMethod to remote server written in Node.js. The code works without any problem if I use stream function. It accepts binary data and writes the file onto local drive without hiccup. However, it gives me "Missing boundary error" when I use Busboy. To resolves that issue, I pass along the boundary to Invoke-RestMethod. That get rids of Boundary error, but Busboy does not fire up file event at all. I've been scratching my head and trying to figure out two days already and the solution seems to elude me. The very same code was working just fine couple of weeks ago, but it does not anymore. I'm not sure if any changes has been made to work environment but very weird.

Stream code: This works just fine

Server code

fs = require('fs');
server = restify.createServer();
server.post('/file',restify.queryParser(),uploadFile);    
function uploadFile(req, res, next) {   
    var wstream = fs.createWriteStream("x.jpg");
    req.pipe(wstream);
}

Powershell

$upload= Invoke-RestMethod -Uri "http://localhost:8088/file" -Method Post -InFile $imagePath -ContentType 'multipart/form-data'

Busboy code: This throws Missing Boundary error

Server code

fs = require('fs');
server = restify.createServer();
server.post('/file',restify.queryParser(),uploadFile);    
function uploadFile(req, res, next) {   
    var fileStream = new BusBoy({ headers: req.headers });  
}

Powershell

$upload= Invoke-RestMethod -Uri "http://localhost:8088/file" -Method Post -InFile $imagePath -ContentType 'multipart/form-data'

Powershell code with boundary set and modified Node.js code. "On file" does not get called.

Powershell

$begin = @"
---BlockBoundary---
"@

$end = @"
---BlockBoundary---
"@

Add-Content 'RequestBodySavedToFile' $begin
$imageData = Get-Content $imagePath -Raw -Encoding Byte
Add-Content 'RequestBodySavedToFile' $imageData -Encoding Byte
Add-Content 'RequestBodySavedToFile' $end

$url = "http://localhost:8088/file"
$contentType = "multipart/form-data; boundary=$begin"
$upload= Invoke-RestMethod -Uri $url1 -Method Post -InFile "RequestBodySavedToFile" -ContentType $contentType

Server Code

fs = require('fs');
server = restify.createServer();
server.post('/file',restify.queryParser(),uploadFile);    
function uploadFile(req, res, next) {   
    var fileStream = new BusBoy({ headers: req.headers });                      

    req.pipe(fileStream);       

    fileStream.on('file', function(fieldname, file, filename, encoding, mimetype) {
        console.log('File [' + fieldname + ']: filename: ' + filename + ', encoding: ' + encoding + ', mimetype: ' + mimetype); 
        res.end();      
    });
}

Any idea what causing this? I appreciate much for all input.

Upvotes: 2

Views: 809

Answers (1)

mscdex
mscdex

Reputation: 106736

The reason there are no file events is because the request data is not correctly formatted according to multipart/form-data (you're at least missing the appropriate headers for each part).

Upvotes: 1

Related Questions