Skip to content

Instantly share code, notes, and snippets.

@Fire-
Last active July 12, 2017 10:20
Show Gist options
  • Save Fire-/aceefb701b57b7ba718a to your computer and use it in GitHub Desktop.
Save Fire-/aceefb701b57b7ba718a to your computer and use it in GitHub Desktop.
example for uploading files to Hipchat with the v2 api with node.js and the Request package
var request = require('request'), //ezmode
fs = require('fs');
var uploadFileName = 'tit.jpg'; // this is a local file, but could be any file or data you want to read in
//ITS A BIRD I SWEAR
fs.readFile('./' + uploadFileName, function read(err, data) {
// hipchat appears to expect the file as a byte array
// and does not support Content-Transfer-Encoding at the time of this gist's creation
request.post({
'url': 'https://api.hipchat.com/v2/room/{ID_OR_NAME}/share/file',
'headers': {
'Authorization': 'Bearer {auth_token}'
},
'multipart': [{
'Content-Type': 'application/json; charset=UTF-8',
'Content-Disposition': 'attachment; name="metadata"',
'body': JSON.stringify({
'message': "this whole object is optional"
})
}, {
'Content-Type': '{file/mimetype}',
'Content-Disposition': 'attachment; name="file"; filename="{filename.extension}"',
'body': data
}]
}, function (err, results) {
if (!err) {
console.log(results);
} else {
console.error(err, results);
console.error('---error');
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment