2 years ago
#75354
Ilya Sunzo
Request data is lost during proxying in Laravel
I am developing an API gateway in Laravel, which proxies all the front-end requests to microservices. In one of the cases, it is necessary to proxy a request that contains a zip archive. Now I'm proxying the request as follows.
declare(strict_types=1);
namespace App\Gateway\Core;
use GuzzleHttp\Client;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
class ProxyAction
{
private Client $client;
private UriSwapper $swapper;
public function __construct(Client $client, UriSwapper $swapper)
{
$this->client = $client;
$this->swapper = $swapper;
}
public function __invoke(ServerRequestInterface $request): ResponseInterface
{
$uri = $this->swapper->swap($request->getUri());
return $this->client->sendRequest($request->withUri($uri));
}
}
Using DI, I cast the request to the ServerRequestInterface, replaced the URI with the target one, and sent the request further. The problem is that a correct request with all the data and a request without data (neither file nor body) comes to the microservice. The only way to send a request in which the data would not come off on the way is to use the HTTP facade and its Htpp::attach method.
Http::attach($fileName, $file->getContent(), $file->getFilename())
->post($uri, $body);
Perhaps someone has encountered such a problem or has guesses what the problem might be? I have been struggling with this problem for four days.
php
laravel
proxy
0 Answers
Your Answer