Bläddra i källkod

change PUT & PATCH requests to POST with _method parameter if the request is multipart/from-data

Ahmed Fathy 2 år sedan
förälder
incheckning
a91c0d0f9b
1 ändrade filer med 16 tillägg och 2 borttagningar
  1. 16 2
      src/Writing/PostmanCollectionWriter.php

+ 16 - 2
src/Writing/PostmanCollectionWriter.php

@@ -101,13 +101,27 @@ class PostmanCollectionWriter
 
     protected function generateEndpointItem(OutputEndpointData $endpoint): array
     {
+        $method = $endpoint->httpMethods[0];
+
+        $bodyParameters = empty($endpoint->bodyParameters) ? null : $this->getBodyData($endpoint);
+
+        if ((in_array('PUT', $endpoint->httpMethods) || in_array('PATCH', $endpoint->httpMethods))
+            && isset($bodyParameters['formdata'])) {
+            $method = 'POST';
+            $bodyParameters['formdata'][] = [
+                'key' => '_method',
+                'value' => $endpoint->httpMethods[0],
+                'type' => 'text',
+            ];
+        }
+
         $endpointItem = [
             'name' => $endpoint->metadata->title !== '' ? $endpoint->metadata->title : $endpoint->uri,
             'request' => [
                 'url' => $this->generateUrlObject($endpoint),
-                'method' => $endpoint->httpMethods[0],
+                'method' => $method,
                 'header' => $this->resolveHeadersForEndpoint($endpoint),
-                'body' => empty($endpoint->bodyParameters) ? null : $this->getBodyData($endpoint),
+                'body' => $bodyParameters,
                 'description' => $endpoint->metadata->description,
             ],
             'response' => $this->getResponses($endpoint),