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

Problem courtesy of: mike-r


I achieved the goal which were described in question: to send POST request from node.js with *.zip/*.wgt file to the other server.

I did not managed that with the aproach introduced in the question: to build request step-by-step using request module. I do some reaserch and I found intersting node.js module: form-data . What does this modul do? Readme.md says:

A module to create readable "multipart/form-data" streams. Can be used to submit forms and file uploads to other web applications.

In part where example where listed I found following example:

var CRLF = '\r\n'; var form = new FormData(); var options = { header: CRLF + '--' + form.getBoundary() + CRLF + 'X-Custom-Header: 123' + CRLF + CRLF, knownLength: 1 }; form.append('my_buffer', buffer, options); form.submit('http://example.com/', function(err, res) { if (err) throw err; console.log('Done'); });

In the example above there is a FormData() object created, data is appended to the form and then form is submited. Additionally there is a custom hedear created.

I merged the code which I presented in question with the code from above presented example and got the code which resolved my problem:

var formData = require('form-data'); var fs = require('fs'); var CRLF = '\r\n'; var form = new FormData(); var options = { header: '--' + form.getBoundary() + CRLF + 'Content-Disposition: form-data; name="file";'+ 'filename="bubbles.wgt"'+ CRLF + 'Content-Type: application/octet-stream' + CRLF + CRLF }; form.append('file',fs.readFileSync('./uploads/bubbles.wgt'),options); form.submit({ host: host, port: '8080', path: '/wookie/widgets', auth: 'java:java' }, function(err, res) { if (err) throw err; console.log('Done'); console.log(res); });

I hope it will be helpful to the others.

Cheers, Michal

Solution courtesy of: mike-r

本文前端(javascript)相关术语:javascript是什么意思 javascript下载 javascript权威指南 javascript基础教程 javascript 正则表达式 javascript设计模式 javascript高级程序设计 精通javascript javascript教程

本文标题:Sendindg the POST request from node.js server with *.zip/*.wgt file to the Wooki ...

技术大类 技术大类 | 前端(javascript) | 评论(0) | 阅读(44)