diff --git a/src/storage_manager/model/RemoteStorage.js b/src/storage_manager/model/RemoteStorage.js index aa30f3fd0..a5d9cf43e 100644 --- a/src/storage_manager/model/RemoteStorage.js +++ b/src/storage_manager/model/RemoteStorage.js @@ -74,7 +74,15 @@ module.exports = require('backbone').Model.extend({ }, load(keys, clb) { - this.request(this.get('urlLoad'), {body: {keys}, method: 'get'}, clb); + let queryString = ''; + keys.forEach(function(key, index, keys) { + queryString += key; + if (index < keys.length - 1) { + queryString += ','; + } + }, this); + + this.request(`${this.get('urlLoad')}?keys=${queryString}`, {method: 'get'}, clb); }, /** @@ -91,6 +99,8 @@ module.exports = require('backbone').Model.extend({ const reqHead = 'X-Requested-With'; const typeHead = 'Content-Type'; const bodyObj = opts.body || {}; + const bodilessMethods = ['get', 'head', 'options', 'delete']; + let fetchOptions; let body; for (let param in params) { @@ -117,14 +127,19 @@ module.exports = require('backbone').Model.extend({ body.append(bodyKey, bodyObj[bodyKey]); } } - - this.onStart(); - this.fetch(url, { + fetchOptions = { method: opts.method || 'post', credentials: 'include', headers, - body, - }).then(res => (res.status/200|0) == 1 ? + }; + + // Body should not be included on GET, HEAD, OPTIONS or DELETE methods + if (!bodilessMethods.includes(fetchOptions.method)) { + fetchOptions.body = body; + } + + this.onStart(); + this.fetch(url, fetchOptions).then(res => (res.status/200|0) == 1 ? res.text() : res.text().then((text) => Promise.reject(text) )) diff --git a/src/utils/fetch.js b/src/utils/fetch.js index 0653daf4c..a694d7e65 100644 --- a/src/utils/fetch.js +++ b/src/utils/fetch.js @@ -5,6 +5,7 @@ window.Promise = window.Promise || Promise; export default typeof fetch == 'function' ? fetch.bind() : (url, options) => { return new Promise((res, rej) => { const req = new XMLHttpRequest(); + const bodilessMethods = ['get', 'head', 'options', 'delete']; req.open(options.method || 'get', url); req.withCredentials = options.credentials == 'include'; @@ -24,6 +25,11 @@ export default typeof fetch == 'function' ? fetch.bind() : (url, options) => { req.upload.onprogress = options.onProgress; } - req.send(options.body); + if (bodilessMethods.includes(options.method)) { + req.send(); + } else { + req.send(options.body); + } + }); } diff --git a/test/specs/storage_manager/model/Models.js b/test/specs/storage_manager/model/Models.js index 25094c678..3afc26173 100644 --- a/test/specs/storage_manager/model/Models.js +++ b/test/specs/storage_manager/model/Models.js @@ -98,7 +98,7 @@ module.exports = { obj.load(['item1', 'item2']); const callResult = obj.fetch; expect(callResult.called).toEqual(true); - expect(callResult.firstCall.args[0]).toEqual(endpointLoad); + expect(callResult.firstCall.args[0]).toEqual(`${endpointLoad}?keys=item1,item2`); }); });