I'm trying to post a photo using mikeal request library, but the post comes emp
request = require('request')
fs = require("fs")
fs.createReadStream('zebra.jpg').pipe(request.post('http://localhost:2000'))
(on localhost:2000 I've got a simple echo for now)
Now, this works but I want to pass additional parameters using standard POST format.
What I'm actually trying to do is to post an image to Facebook via API, which means I'd like to include a title and possibly some more fields.
If streaming is not the right approach (although I see many benefits such as getting away without temporary files and buffers), what would be the right one?
Thanks for ideas.
UPD:
I've got this far:
fs.createReadStream('zebra.jpg').pipe(graph.post('418533674856800/photos',
{message:"I'm a new API photo!", name:"API Photo",privacy:{value:"EVERYONE"}},
function(err, res) {
console.log(res);
}));
but it returns
dest.on('drain', ondrain);
^
TypeError: Object #<Graph> has no method 'on'
at [object Object].pipe (stream.js:52:8)
at Request._callback (c:\My Stuff\Creatiff\PRAGmatiki\Web-node.js\postaspage.js:66:36)
at Request.callback (c:\My Stuff\Creatiff\PRAGmatiki\Web-node.js\node_modules\request\main.js:119:22)
at Request.<anonymous> (native)
at Request.emit (events.js:70:17)
at Request.<anonymous> (c:\My Stuff\Creatiff\PRAGmatiki\Web-node.js\node_modules\request\main.js:521:16)
at Request.emit (events.js:67:17)
at IncomingMessage.<anonymous> (c:\My Stuff\Creatiff\PRAGmatiki\Web-node.js\node_modules\request\main.js:483:14)
at IncomingMessage.emit (events.js:88:20)
at HTTPParser.parserOnMessageComplete [as onMessageComplete] (http.js:130:23)
Is this happening because I'm streaming? Any help please!
var path = require('path'),
mime = require('mime');
request({
url: 'http://localhost:2000',
headers: {
'content-type' : 'multipart/form-data'
},
method: 'POST',
multipart: [{
'Content-Disposition' : 'form-data; name="inputname"; filename="' + path.basename('zebra.jpg') + '"',
'Content-Type' : mime.lookup('zebra.jpg'),
body: fs.readFileSync('zebra.jpg')
},{
'Content-Disposition' : 'form-data; name="input[array]"; filename="' + path.basename('zebra1.jpg') + '"',
'Content-Type' : mime.lookup('zebra1.jpg'),
body: fs.readFileSync('zebra1.jpg')
},{
'Content-Disposition' : 'form-data; name="input[array]"; filename="' + path.basename('zebra2.jpg') + '"',
'Content-Type' : mime.lookup('zebra2.jpg'),
body: fs.readFileSync('zebra2.jpg')
},{
'Content-Disposition' : 'form-data; name="text"',
body: "text input"
}]
},
function(err, res, body){
});
I don't know what graph
is (doesn't appear in mikeael's documentation), but it doesn't implement the Stream
interface so can't be used with pipe()
.
To send multiple parts in a POST you need to use a request of type multipart/form-data
. The latest version of mikeal/request has experimental support for this (with examples). Other modules also support it (needle for example, though stream support was a little lacking last time I looked).