Using node and repeater for POST messages

I can upload the file using restler.file to the data section without problems. Now I'm trying to write a very short CSV data string that I cannot find in the documentation for the data function, but after reading the code, I thought it was right:

restler.post("http://posttestserver.com/post.php", { multipart: true, data: { "upload": restler.data("people.csv", "text/csv", '384;213;Status Update'), "returnURL": "" } }).on("complete", function(data) { console.log(data); }); 

Unfortunately, this just hangs and there will be a timeout. I tried adding EOF and other things to the 3rd argument, but I know something is missing. The data line above is the same content as the file that works when I use the restler.file file. I would prefer not to write the CSV file if I do not need it before sending.

0
post multipartform-data restler
Mar 21 '13 at 4:03
source share
1 answer

EDIT ----

According to @Joni's comment on the question above, this problem seems to have been fixed after the patch was sent via a transfer request .

Original answer (from OP) ----

From the study of the restler (and corresponding with the maintainer) it does not look like the restler can do what I wanted. Note. Someone made some code that would allow parts of the file as a stream, but it was not accepted into the branch, and I do not have enough experience with streams.

I solved the problem by returning to the basics. I read the RFC for multipart ( http://www.ietf.org/rfc/rfc2388.txt ) and found that there are only a few rules in building a body, mostly some extra \ r \ n and '-' in the right places.

I decided to just format the raw POST body and send it through the main node http client.

This worked:

 var http = require('http'); postBody = new Buffer( '------WebKitFormBoundaryebFz3Q3NHxk7g4qY' + "\r\n" + 'Content-Disposition: form-data; name="upload"; filename="filename.csv"' + "\r\n" + 'Content-Type: text/csv' + "\r\n" + '\r\n' + 'comma,separated,values' + "\r\n" + '------WebKitFormBoundaryebFz3Q3NHxk7g4qY' + "\r\n" + 'Content-Disposition: form-data; name="returnUrl"' + "\r\n" + '\r\n' + 'http://return.url/' + "\r\n" + '------WebKitFormBoundaryebFz3Q3NHxk7g4qY--' ); var headers = { "Content-Type": "multipart/form-data; boundary=----WebKitFormBoundaryebFz3Q3NHxk7g4qY", "Content-Length": postBody.length }; //These are the post options var options = { hostname: 'myhost.com', port: 80, path: '/myPost', method: 'POST', headers: headers }; // so we can see that things look right console.log("postBody:\n" + postBody); console.log("postBody.length:\n" + postBody.length); var responseBody = ''; // set up the request and the callbacks to handle the response data var request = http.request(options, function(response) { // when we receive data, store it in a string response.on('data', function (chunk) { responseBody += chunk; }); // at end the response, run a function to do something with the response data response.on('end',function() { console.log(responseBody); }); }); // basic error function request.on('error', function(e) { console.log('problem with request: ' + e.message); }); // write our post body to the request request.write(postBody); // end the request request.end(); 

I hope this helps people doing multipart / form-data.

+5
Mar 21 '13 at 21:56
source share



All Articles