Proxied "multipart" request results in empty body

293 Views Asked by At

Our team recently migrated to Laminas and in doing so, are now experiencing an issue with how our application handles file uploads. We have 2 applications (both MVC): one view application and one API application. Our view application handles all requests from the browser and proxies API request through our ApiProxyController. This works for every one of our requests except for our file uploads. For those requests (multipart/form-data;), we've seen that by the time the request gets to our API application, the body of the request is empty.

We've experienced this issue using both the latest version of the Guzzle HTTP client and the Laminas client. Here is a snippet of the code we're using to proxy the request from our view application:

/* @var $request \Laminas\Http\PhpEnvironment\Request */
$request = $this->getRequest();
// URI object used to build PSR-7 request; update URI for proper forwarding

$uri = $request->getUri();
$uri->setHost($this->apiHost);

$psr7Request = Psr7ServerRequest::fromLaminas($request)
    ->withoutHeader('Host')
    ->withHeader('Authorization', sprintf('Bearer %s', $identity->getAccessToken()->getToken()));

$guzzle = new \GuzzleHttp\Client([
    // Turn off SSL certificate verification
    'verify' => false,
    // Prevent exceptions from being thrown due to HTTP status codes (e.g. 4xx, 5xx)
    'exceptions' => false,
]);

$psr7Response = $guzzle->send($psr7Request);
$response = Psr7Response::toLaminas($psr7Response);
1

There are 1 best solutions below

0
json_floyd On

I had similar trouble in laravel. You need to remove content-type header from original request since it contains boundary. Then you should modify your request to send body as multipart to generate new content-type and content-length headers automatically.