Skip to content

Commit

Permalink
Fix Fetch requests using FormData not sending the actual data to the …
Browse files Browse the repository at this point in the history
…server.

Fixes #64

When running the requests using the original fetch implementation, we passed in XhookFormData instead of the native FormData object, causing the request to be sent with [Object object] instead of the real data.
  • Loading branch information
morsdyce committed Aug 2, 2017
1 parent d70307c commit 93879b7
Show file tree
Hide file tree
Showing 7 changed files with 3,082 additions and 2 deletions.
5 changes: 4 additions & 1 deletion dist/xhook.js
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,9 @@ if (typeof WINDOW[FETCH] === "function") {
return new Promise(function(resolve, reject) {
var done, getRequest, processAfter, processBefore, send;
getRequest = function() {
if (options.body instanceof XHookFormData) {
options.body = options.body.fd;
}
if (options.headers) {
options.headers = new Headers(options.headers);
}
Expand Down Expand Up @@ -669,4 +672,4 @@ if (typeof define === "function" && define.amd) {
(this.exports || this).xhook = xhook;
}

}.call(this));
}.call(this));
2 changes: 1 addition & 1 deletion dist/xhook.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 35 additions & 0 deletions example/fetch-form-data.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<h5>Fetch with form data</h5>

<form>
<input type="text" id="username" name="username">
<input type="password" id="password" name="password">
<button id="submit" type="button">submit</button>
</form>

<pre id="result"></pre>

<script src="https://cdn.jsdelivr.net/fetch/2.0.1/fetch.min.js"></script>
<script src="../dist/xhook.js"></script>
<script type="text/javascript">
xhook.after(function (request, response) {
console.log(request, response);
});

document.getElementById("submit").addEventListener("click", function () {
var username = document.getElementById("username").value;
var password = document.getElementById("password").value;
const data = new FormData();
data.append('username', username);
data.append('password', password);
console.log("submitting data,", data);
fetch(`http://echo.jpillora.com/cors-test`, {
method: 'POST',
credentials: 'same-origin',
body: data
}).then((response) => {
response.text().then((text) => {
document.querySelector('#result').innerHTML = text;
})
})
});
</script>
1 change: 1 addition & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
<select>
<option value="vanilla">With Vanilla</option>
<option value="vanilla-fetch">With Fetch</option>
<option value="fetch-form-data">With Fetch & FormData</option>
<option value="vanilla-both">Vanilla With Fetch</option>
<option value="xhr-webworker">With WebWorker</option>
<option value="fetch-webworker">With Fetch in WebWorker</option>
Expand Down
Loading

0 comments on commit 93879b7

Please sign in to comment.