Skip to content

Instantly share code, notes, and snippets.

@sekoyo
Created December 20, 2013 14:46
Show Gist options
  • Save sekoyo/8055724 to your computer and use it in GitHub Desktop.
Save sekoyo/8055724 to your computer and use it in GitHub Desktop.
Send a query to phabricator using node.js
var ApiQuery = function() {
this.http = require('http');
this.q = require('q');
this.Connect = require('./conduit.connect');
this.initialize();
};
ApiQuery.prototype = {
initialize: function() {
this.connect = new this.Connect();
},
send: function(api, params) {
params = params || {};
var deferred = this.q.defer();
console.log('Received send:', api, params);
this.connect.toFirst().then(function(attrs) {
console.log('Succesfully authed');
params.__conduit__ = {
connectionID: attrs.connectionID,
sessionKey: attrs.sessionKey
};
var formData = 'params='+JSON.stringify(params)+'&output=json&__conduit__=1',
postOptions = {
host: attrs.host.host,
port: 80,
path: '/api/' + api,
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': Buffer.byteLength(formData)
}
},
postReq = this.http.request(postOptions, function(res) {
console.log('Send response:', res);
res.setEncoding('utf8');
res.on('error', function(e) {
console.log('Error sending post:', e);
});
res.on('data', function(data) {
console.log('Received send data:', data);
deferred.resolve(data);
});
});
postReq.end(formData, 'utf8');
});
return deferred.promise;
}
};
module.exports = ApiQuery;
@sekoyo
Copy link
Author

sekoyo commented Dec 20, 2013

Note: formData doesn't use something like qs.stringify on the whole object as Phabricator strangely expects only the stuff in params to be in quotes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment