From f36e9e40f6f45a924b3c52f9dc64e3547af7ddd3 Mon Sep 17 00:00:00 2001 From: David Graham Date: Sat, 1 Nov 2014 17:26:22 -0600 Subject: [PATCH] Remove form encoded object body. This was based on a misreading of the URLSearchParams portion of section 5.2 of https://fetch.spec.whatwg.org/#body-mixin. Use FormData as the request body to POST form fields instead. --- README.md | 12 ------------ fetch.js | 25 +------------------------ 2 files changed, 1 insertion(+), 36 deletions(-) diff --git a/README.md b/README.md index 9be474c..b1c9132 100644 --- a/README.md +++ b/README.md @@ -102,18 +102,6 @@ fetch('/query', { }) ``` -### Post form fields - -```javascript -fetch('/query', { - method: 'post', - body: { - name: 'Hubot', - login: 'hubot' - } -}) -``` - ### Post JSON ```javascript diff --git a/fetch.js b/fetch.js index a261709..186dc58 100644 --- a/fetch.js +++ b/fetch.js @@ -120,15 +120,6 @@ this.referrer = null } - function encode(params) { - return Object.getOwnPropertyNames(params).filter(function(name) { - return params[name] !== undefined - }).map(function(name) { - var value = (params[name] === null) ? '' : params[name] - return encodeURIComponent(name) + '=' + encodeURIComponent(value) - }).join('&').replace(/%20/g, '+') - } - function decode(body) { var form = new FormData() body.trim().split('&').forEach(function(bytes) { @@ -142,15 +133,6 @@ return form } - function isObject(value) { - try { - return Object.getPrototypeOf(value) === Object.prototype - } catch (ex) { - // Probably a string literal. - return false - } - } - function headers(xhr) { var head = new Headers() var pairs = xhr.getAllResponseHeaders().trim().split('\n') @@ -190,12 +172,7 @@ }) }) - var body = self.body - if (isObject(self.body)) { - xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8') - body = encode(self.body) - } - xhr.send(body) + xhr.send(self.body) }) } -- GitLab