diff --git a/Classes/Infrastructure/DeepL/DeepLTranslationService.php b/Classes/Infrastructure/DeepL/DeepLTranslationService.php index 24ec3b2..b8227e4 100644 --- a/Classes/Infrastructure/DeepL/DeepLTranslationService.php +++ b/Classes/Infrastructure/DeepL/DeepLTranslationService.php @@ -115,8 +115,7 @@ public function translate(array $texts, string $targetLanguage, ?string $sourceL $body .= '&text=' . urlencode($part); } - $apiRequest = $this->createRequest('translate', 'POST'); - $apiRequest->withBody($this->streamFactory->createStream($body)); + $apiRequest = $this->createRequest('translate', 'POST', $body); $browser = $this->getBrowser(); @@ -239,20 +238,29 @@ protected function getBrowser(): Browser } /** - * @param string $method - * @param string $endpoint + * @param string $endpoint + * + * @param string $method + * @param string|null $body * * @return ServerRequestInterface */ protected function createRequest( string $endpoint, - string $method = 'GET' + string $method = 'GET', + string $body = null ): ServerRequestInterface { $deeplAuthenticationKey = $this->getDeeplAuthenticationKey(); $baseUri = $deeplAuthenticationKey->isFree ? $this->settings['baseUriFree'] : $this->settings['baseUri']; - return $this->serverRequestFactory->createServerRequest($method, $baseUri . $endpoint) + $request = $this->serverRequestFactory->createServerRequest($method, $baseUri . $endpoint) ->withHeader('Accept', 'application/json') ->withHeader('Authorization', sprintf('DeepL-Auth-Key %s', $deeplAuthenticationKey->authenticationKey)) ->withHeader('Content-Type', 'application/x-www-form-urlencoded'); + + if ($body) { + $request = $request->withBody($this->streamFactory->createStream($body)); + } + + return $request; } }