Browse Source

Merge pull request #573 from jfcherng/feat/support-application-json

Make Http Request supports application/json
walkor 5 years ago
parent
commit
418b3ae818
1 changed files with 7 additions and 3 deletions
  1. 7 3
      Protocols/Http/Request.php

+ 7 - 3
Protocols/Http/Request.php

@@ -458,13 +458,17 @@ class Request
             $this->_data['post'] = static::$_postCache[$body_buffer];
             $this->_data['post'] = static::$_postCache[$body_buffer];
             return;
             return;
         }
         }
-        $content_type = $this->header('content-type');
-        if ($content_type !== null && \preg_match('/boundary="?(\S+)"?/', $content_type, $match)) {
+        $content_type = $this->header('content-type', '');
+        if (\preg_match('/boundary="?(\S+)"?/', $content_type, $match)) {
             $http_post_boundary = '--' . $match[1];
             $http_post_boundary = '--' . $match[1];
             $this->parseUploadFiles($http_post_boundary);
             $this->parseUploadFiles($http_post_boundary);
             return;
             return;
         }
         }
-        \parse_str($body_buffer, $this->_data['post']);
+        if (\preg_match('/\bjson\b/i', $content_type)) {
+            $this->_data['post'] = (array) json_decode($body_buffer, true);
+        } else {
+            \parse_str($body_buffer, $this->_data['post']);
+        }
         if ($cacheable) {
         if ($cacheable) {
             static::$_postCache[$body_buffer] = $this->_data['post'];
             static::$_postCache[$body_buffer] = $this->_data['post'];
             if (\count(static::$_postCache) > 256) {
             if (\count(static::$_postCache) > 256) {