From cc145d5c370f3f2f9be6b26953ee70da0f930e5f Mon Sep 17 00:00:00 2001 From: Eno <895183594@qq.com> Date: Tue, 30 Sep 2025 11:38:54 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96http=E8=A1=A8=E5=8D=95POST?= =?UTF-8?q?=E8=AF=B7=E6=B1=82getParsedBody=E5=AF=B9=E5=8E=9F=E5=A7=8B?= =?UTF-8?q?=E8=AF=B7=E6=B1=82=E4=BD=93=E7=9A=84=E5=8A=A0=E8=BD=BD=EF=BC=9A?= =?UTF-8?q?=E7=A7=BB=E9=99=A4=E5=9C=A8getParsedBody=E5=AF=B9=E5=8E=9F?= =?UTF-8?q?=E5=A7=8Bbody=E5=8A=A0=E8=BD=BD=E7=9A=84=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Util/Http/ServerRequest.php | 9 ++------- .../Component/Tests/Util/Http/ServerRequestTest.php | 13 +++---------- 2 files changed, 5 insertions(+), 17 deletions(-) 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);