Przeglądaj źródła

Merge pull request #257 from urbsny/patch-4

Fix sending PUT or PATCH with FormData.
Shalvah 4 lat temu
rodzic
commit
0b9b64afc5
1 zmienionych plików z 9 dodań i 2 usunięć
  1. 9 2
      resources/js/tryitout.js

+ 9 - 2
resources/js/tryitout.js

@@ -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)
         })
@@ -206,4 +213,4 @@ async function executeTryOut(endpointId, form) {
         .finally(() => {
             executeBtn.textContent = "Send Request 💥";
         });
-}
+}