Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Buffer support for Multipart generation #2

Merged
merged 1 commit into from

2 participants

Derek Lindahl Tomás Pollak
Derek Lindahl

I recently needed the ability to accept a Base64 input and immediately pass it on to a 3rd party service.

Rather than writing the data to disk just to send it off and delete it, I added the ability to pass the raw file contents directly to Needle so that it can properly encode the data in its Multipart construction.

This can be done by specifying a buffer attribute (as opposed to the file file path attribute):

var base64_input = new Buffer(form_image_input.replace(/^data:image\/\w+;base64,/, ""), "base64");
var data = {
  foo: bar,
  image: { file: '/home/tomas/linux.png', content_type: 'image/png' },
  gif: { buffer: base64_input, content_type: 'image/gif' }
}
Derek Lindahl dlindahl Added support for Buffers when generating Multipart messages.
This can be done by specifying a `buffer` attribute as opposed to a `file` attribute.
0ed8651
Tomás Pollak
Owner

Looks neat, good job man. I'm merging it but I want to add a few specs before pushing a new release.

Tomás Pollak tomas merged commit f36cf4c into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 4, 2012
  1. Derek Lindahl

    Added support for Buffers when generating Multipart messages.

    dlindahl authored
    This can be done by specifying a `buffer` attribute as opposed to a `file` attribute.
This page is out of date. Refresh to see the latest.
Showing with 20 additions and 7 deletions.
  1. +3 −1 README.md
  2. +13 −5 lib/needle.js
  3. +4 −1 test/test.js
4 README.md
View
@@ -108,9 +108,11 @@ client.post('http://my.app.com', data, function(err, resp, body){
### Multipart POST
``` js
+var base64_input = new Buffer(form_image_input.replace(/^data:image\/\w+;base64,/, ""), "base64");
var data = {
foo: bar,
- image: { file: '/home/tomas/linux.png', content_type: 'image/png' }
+ image: { file: '/home/tomas/linux.png', content_type: 'image/png' },
+ gif: { buffer: base64_input, content_type: 'image/gif' }
}
var options = {
18 lib/needle.js
View
@@ -52,7 +52,7 @@ function flatten(object, into, prefix){
var prefix_key = prefix ? prefix + "[" + key + "]" : key;
var prop = object[key];
- if(prop && typeof prop === "object" && !(prop.file && prop.content_type))
+ if(prop && typeof prop === "object" && !((prop.buffer || prop.file) && prop.content_type))
flatten(prop, into, prefix_key)
else
into[prefix_key] = prop;
@@ -192,7 +192,7 @@ var Needle = {
var value = object[key];
if(value !== null && typeof value != 'undefined'){
- var part = value.file && value.content_type ? value : {value: value};
+ var part = (value.buffer || value.file) && value.content_type ? value : {value: value};
body += this.generate_part(key, part, boundary);
}
@@ -207,10 +207,18 @@ var Needle = {
var return_part = '--' + boundary + "\r\n";
return_part += "Content-Disposition: form-data; name=\"" + name + "\"";
- if(part.file && part.content_type){
+ if((part.file || part.buffer) && part.content_type){
- var filename = path.basename(part.file);
- var data = fs.readFileSync(part.file);
+ var filename,
+ data;
+
+ if(part.file){
+ filename = path.basename(part.file);
+ data = fs.readFileSync(part.file);
+ } else {
+ filename = part.filename;
+ data = part.buffer;
+ }
return_part += "; filename=\"" + filename + "\"\r\n";
return_part += "Content-Type: " + part.content_type + "\r\n\r\n";
5 test/test.js
View
@@ -42,6 +42,8 @@ function multipart_post(){
var data = 'Plain text data.\nLorem ipsum dolor sit amet.\nBla bla bla.\n';
fs.writeFileSync(filename, data);
+ var black_pixel = new Buffer("data:image/gif;base64,R0lGODlhAQABAIAAAAUEBAAAACwAAAAAAQABAAACAkQBADs=".replace(/^data:image\/\w+;base64,/, ""), "base64");
+
var data = {
foo: 'bar',
bar: 'baz',
@@ -50,7 +52,8 @@ function multipart_post(){
even: {
more: 'nesting'
}
- }
+ },
+ buffer: { filename:'black_pixel.gif', buffer:black_pixel, content_type: 'image/gif' },
}
client.post('http://posttestserver.com/post.php?dir=example', data, {multipart: true}, function(err, resp, body){
Something went wrong with that request. Please try again.