I am able to upload a file using restler.file in the data section with no problem. I am now trying to write a very short CSV data string, which I am not able to find documentation for the data function, but reading the code I thought I had it correct:
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 will time out. I tried adding EOF and other things to the 3rd arg but I know I am missing something. The data string I have above is the exact same contents as the file that works when I use restler.file. I would rather not have to write out a CSV file if I don't have to before POSTing it.
EDIT ----
As per @Joni's comment to the question above, this problem seems to have been rectified after a fix was submitted via pull request.
Original Answer (from OP) ----
From the research on restler (and corresponding with the maintainer) it doesn't look like restler can do what I wanted. Note: Someone has committed some code that would allow a file part in the form of a stream, but it hasn't been accepted into the branch and I don't have enough experience with streams.
I solved the problem going back to basics. I read the RFC for multipart (http://www.ietf.org/rfc/rfc2388.txt) and found there are only a few rules to be aware of in constructing the body, mostly some extra \r\n's and '--' in the right places.
I decided to simply format the raw POST body and send it through the basic 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.