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

Solution

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教程

代码区博客精选文章
分页:12
转载请注明
本文标题:Sendindg the POST request from node.js server with *.zip/*.wgt file to the Wooki ...
本站链接:https://www.codesec.net/view/621367.html


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 前端(javascript) | 评论(0) | 阅读(35)