diff --git a/src/Util/Http/ServerRequest.php b/src/Util/Http/ServerRequest.php index 331aac8791..439735121c 100644 --- a/src/Util/Http/ServerRequest.php +++ b/src/Util/Http/ServerRequest.php @@ -286,11 +286,6 @@ public function getParsedBody() $parsedBody = &$this->parsedBody; if (null === $parsedBody) { - if (!$this->bodyInited) - { - $this->initBody(); - $this->bodyInited = true; - } $contentType = $this->getHeaderLine(RequestHeader::CONTENT_TYPE); if ('' === $contentType) { @@ -311,7 +306,7 @@ public function getParsedBody() // json elseif (MediaType::APPLICATION_JSON === $contentType) { - $content = $this->body->getContents(); + $content = $this->getBody()->getContents(); if ('' !== $content) { $parsedBody = json_decode($content, !Config::get('@currentServer.jsonBodyIsObject', false), 512, \JSON_THROW_ON_ERROR); @@ -331,7 +326,7 @@ public function getParsedBody() ])) { $this->post = $parsedBody = new \DOMDocument(); - $parsedBody->loadXML($this->body->getContents()); + $parsedBody->loadXML($this->getBody()->getContents()); } // 其它 else diff --git a/tests/unit/Component/Tests/Util/Http/ServerRequestTest.php b/tests/unit/Component/Tests/Util/Http/ServerRequestTest.php index a8c3df78ea..6de87b613c 100644 --- a/tests/unit/Component/Tests/Util/Http/ServerRequestTest.php +++ b/tests/unit/Component/Tests/Util/Http/ServerRequestTest.php @@ -144,21 +144,14 @@ private function __testParsedBody(string $type): void $this->assertEquals(['name' => 'imi'], $request->getParsedBody()); // form - $request = new class() extends ServerRequest { - /** - * {@inheritDoc} - */ - protected function initBody(): void - { - $this->post = ['name' => 'imi']; - } - }; + $request = new ServerRequest(); foreach ([ MediaType::APPLICATION_FORM_URLENCODED, MediaType::MULTIPART_FORM_DATA, ] as $contentType) { - $request->setHeader(RequestHeader::CONTENT_TYPE, $contentType); + $request->setPost(['name' => 'imi']) + ->setHeader(RequestHeader::CONTENT_TYPE, $contentType); $requestTmp = $request->withMethod(RequestMethod::POST); $this->assertEquals(['name' => 'imi'], $requestTmp->getParsedBody()); $requestTmp = $request->withMethod(RequestMethod::PUT);