diff --git a/src/lib/api.js b/src/lib/api.js index 0f8df9d41..05cc2bf1d 100644 --- a/src/lib/api.js +++ b/src/lib/api.js @@ -18,7 +18,7 @@ module.exports = function (opts, callback) { defaults(opts, { host: process.env.API_HOST, headers: {}, - json: {}, + responseType: 'json', useCsrf: false }); @@ -33,6 +33,11 @@ module.exports = function (opts, callback) { .join(opts.uri.indexOf('?') === -1 ? '?' : '&'); } + if (opts.formData) { + opts.body = urlParams(opts.formData); + opts.headers['Content-Type'] = 'application/x-www-form-urlencoded'; + } + var apiRequest = function (opts) { if (opts.host !== '') { // For IE < 10, we must use XDR for cross-domain requests. XDR does not support @@ -69,7 +74,6 @@ module.exports = function (opts, callback) { if (opts.useCsrf) { jar.use('scratchcsrftoken', '/csrf_token/', function (err, csrftoken) { if (err) return log.error('Error while retrieving CSRF token', err); - opts.json.csrftoken = csrftoken; opts.headers['X-CSRFToken'] = csrftoken; apiRequest(opts); }.bind(this)); diff --git a/src/lib/url-params.js b/src/lib/url-params.js index 0fcab02e8..30569f67b 100644 --- a/src/lib/url-params.js +++ b/src/lib/url-params.js @@ -6,7 +6,8 @@ module.exports = function urlParams (values) { return Object .keys(values) .map(function (key) { - return [key, values[key]] + var value = typeof values[key] === 'undefined' ? '' : values[key]; + return [key, value] .map(encodeURIComponent) .join('='); })