Skip to content

Commit

Permalink
request() method now sends params via request body for non-GET requests
Browse files Browse the repository at this point in the history
  • Loading branch information
Hovhannes Babayan committed Jan 2, 2015
1 parent 2ea38d6 commit 9640d31
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 25 deletions.
6 changes: 3 additions & 3 deletions dist/attask.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion examples/browser/login.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
var username = document.getElementById('username').value;
var password = document.getElementById('password').value;

var instance = new window.AtTask.Api({url: url, version: '5.0'});
var instance = new window.AtTask.Api({url: url, version: '4.0'});
instance.login(username, password).then(function(data){
console.log(data)
}, console.error);
Expand Down
35 changes: 15 additions & 20 deletions src/Api.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,25 +35,20 @@ function Api(config) {
this.httpOptions.path = path;
}

var plugins = [
'request',
'login',
'logout',
'search',
'get',
'post',
'put',
'delete',
'report',
'count',
'copy',
'upload',
'execute',
'namedQuery',
'metadata'
];
for(var i=0; i<plugins.length; ++i) {
require('./plugins/' + plugins[i])(Api);
}
require('./plugins/request')(Api);
require('./plugins/login')(Api);
require('./plugins/logout')(Api);
require('./plugins/search')(Api);
require('./plugins/get')(Api);
require('./plugins/post')(Api);
require('./plugins/put')(Api);
require('./plugins/delete')(Api);
require('./plugins/report')(Api);
require('./plugins/count')(Api);
require('./plugins/copy')(Api);
require('./plugins/upload')(Api);
require('./plugins/execute')(Api);
require('./plugins/namedQuery')(Api);
require('./plugins/metadata')(Api);

module.exports = Api;
16 changes: 15 additions & 1 deletion src/plugins/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ var queryString = require('querystring'),
util = require('util');

module.exports = function(Api) {
var requestHasData = function(method) {
return method !== 'GET';
};

Api.prototype.request = function(path, params, fields, method) {
params = params || {};
fields = fields || [];
Expand All @@ -22,7 +26,14 @@ module.exports = function(Api) {

params = queryString.stringify(params);
if (params) {
options.path += '?' + params;
if (requestHasData(options.method)) {
options.headers = options.headers || {};
options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
options.headers['Content-Length'] = Buffer.byteLength(params);
}
else {
options.path += '?' + params;
}
}

var httpTransport = this.httpTransport;
Expand Down Expand Up @@ -52,6 +63,9 @@ module.exports = function(Api) {
});
});
request.on('error', reject);
if (params && requestHasData(options.method)) {
request.write(params);
}
request.end();
});
};
Expand Down

0 comments on commit 9640d31

Please sign in to comment.