Skip to content

Commit

Permalink
Merge pull request #257 from urbsny/patch-4
Browse files Browse the repository at this point in the history
Fix sending PUT or PATCH with FormData.
  • Loading branch information
shalvah authored Jul 1, 2021
2 parents f79f5ba + 64b976a commit 0b9b64a
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions resources/js/tryitout.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,11 +187,18 @@ async function executeTryOut(endpointId, form) {
if (authHeaderEl) headers[authHeaderEl.name] = authHeaderEl.dataset.prefix + authHeaderEl.value;
}
// When using FormData, the browser sets the correct content-type + boundary
let method = form.dataset.method;
if (body instanceof FormData) {
delete headers['Content-Type'];

// When using FormData with PUT or PATCH, send with post and add _method
if (['PUT', 'PATCH'].includes(form.dataset.method)) {
method = 'POST';
setter('_method', form.dataset.method);
}
}

makeAPICall(form.dataset.method, path, body, query, headers, endpointId)
makeAPICall(method, path, body, query, headers, endpointId)
.then(([responseStatus, responseContent, responseHeaders]) => {
handleResponse(endpointId, responseContent, responseStatus, responseHeaders)
})
Expand All @@ -206,4 +213,4 @@ async function executeTryOut(endpointId, form) {
.finally(() => {
executeBtn.textContent = "Send Request 💥";
});
}
}

0 comments on commit 0b9b64a

Please sign in to comment.