浏览代码

Merge pull request #480 from ahmed-aliraqi/master

Change PUT & PATCH requests to POST with _method parameter if the request is multipart/from-data
Shalvah 2 年之前
父节点
当前提交
0607ab9b30
共有 1 个文件被更改,包括 20 次插入2 次删除
  1. 20 2
      src/Writing/PostmanCollectionWriter.php

+ 20 - 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),
@@ -129,6 +143,9 @@ class PostmanCollectionWriter
             case 'multipart/form-data':
                 $inputMode = 'formdata';
                 break;
+            case 'application/x-www-form-urlencoded':
+                $inputMode = 'urlencoded';
+                break;
             case 'application/json':
             default:
                 $inputMode = 'raw';
@@ -138,6 +155,7 @@ class PostmanCollectionWriter
 
         switch ($inputMode) {
             case 'formdata':
+            case 'urlencoded':
                 $body[$inputMode] = $this->getFormDataParams($endpoint->cleanBodyParameters);
                 foreach ($endpoint->fileParameters as $key => $value) {
                     while (is_array($value)) {