mirror of
https://github.com/scratchfoundation/scratch-www.git
synced 2024-11-27 09:35:56 -05:00
Add method to api for submitting forms
Some of our legacy endpoints expect this style rather than json. Also clean up the way useCsrf works — don't always set json attribute to an empty object.
This commit is contained in:
parent
d27b0a2433
commit
26e1ee553b
2 changed files with 8 additions and 3 deletions
|
@ -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));
|
||||
|
|
|
@ -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('=');
|
||||
})
|
||||
|
|
Loading…
Reference in a new issue