I try to send *.wgt file (which is valid *.zip file) with POST request from node.js to other server on which Wookie Server is runinng. I have followed node.js documentation of the http server and fs and two other posts on stackoverflow (Node.js POST File to Server and how to upload a file from node.js) but I did not managed this to work.
What I have already done:
I have *.wgt file which is stored on the node.js server. I create a http server in node, prepare the POST request to the Wookie REST API, get *.zip file stream with fs.createReadStream()
and I stream it with pipe()
. But I get the following error response:
Error: No file found uploaded to the server. The request sent by the client was syntactically incorect (No file uploaded to server).
The Wookie Server API reference for this particular request looks as follows:
POST {wookie}/widgets {file} - Adds a widget to the server. The method echoes the widget metadata in the response.
My code looks as follows:
var http = require('http');
var fs = require('fs');
var file_name = 'test.wgt';
// auth data for access to the wookie server api
var username = 'java';
var password = 'java';
var auth = 'Basic ' + new Buffer(username + ':' + password).toString('base64');
// boundary key for post request
var boundaryKey = Math.random().toString(16);
var wookieResponse = "";
// take content length of the request body: see https://stackoverflow.com/questions/9943010/node-js-post-file-to-server
var body_before_file =
'--' + boundaryKey + '\r\n'
+ 'Content-Type: application/octet-stream\r\n'
+ 'Content-Disposition: form-data; name="file"; filename="'+file_name+'"\r\n'
+ 'Content-Transfer-Encoding: binary\r\n\r\n';
var body_after_file = "--"+boundaryKey+"--\r\n\r\n";
fs.stat('./uploads/'+file_name, function(err, file_info) {
console.log(Buffer.byteLength(body_before_file) +"+"+ file_info.size +"+"+ Buffer.byteLength(body_after_file));
var content_length = Buffer.byteLength(body_before_file) +
file_info.size +
Buffer.byteLength(body_after_file);
// set content length and other values to the header
var header = {
'Authorization': auth,
'Content-Length': String(content_length),
'Accept': '*/*',
'Content-Type': 'multipart/form-data; boundary="--'+boundaryKey+'"',//,
'Accept-Encoding': 'gzip,deflate,sdch',
'Accept-Charset': 'ISO-8859-2,utf-8;q=0.7,*;q=0.3'
};
// set request options
var options = {
host: appOptions.hostWP,
port: 8080,
path: '/wookie/widgets',
method: 'POST',
headers: header
};
// create http request
var requ = http.request(options, function(res) {
res.setEncoding('utf8');
res.on('data', function (chunk) {
wookieResponse = wookieResponse + chunk;
});
res.on('end', function (chunk) {
wookieResponse = wookieResponse + chunk;
console.log(wookieResponse);
})
});
// write body_before_file (see above) to the body request
requ.write(body_before_file);
// prepare createReadStream and pipe it to the request
var fileStream = fs.createReadStream('./uploads/'+file_name);
fileStream.pipe(requ, {end: false});
// finish request with boundaryKey
fileStream.on('end', function() {
requ.end('--' + boundaryKey + '--\r\n\r\n');
});
requ.on('error', function(e) {
console.log('problem with request: ' + e.message);
});
});
What I'm doing wrong and how can I achive correct *.wgt/*.zip file upload with POST request from node.js?
Cheers, Michal