evacch's avatar

Guzzle Http with Maatwebsite Excel Response Content Type

Guzzle version(s) affected: 7.0.1 PHP version: 7.4.6

I have 2 laravel API projects, for example, A and B. The excel export API (/a/export) has to be called in another laravel API (/b/download) with Guzzle/Http. Using Postman, I was testing both APIs and the "/a/export" API was working fine with response type application/vnd.openxmlformats-officedocument.spreadsheetml.sheet returned but if I called "/b/download" which I am using Guzzle/Http, I am getting response type text/html in return.

Because I am using Postman for the excel export API testing. I have the response data as shown in the picture below. image After I press "Save Response to a file", it's giving me a html file (response.html) instead of a excel file (the filename should be "transactions_2021-02-27.xlsx") with defined filename. I have checked the response Content-Type header, it's returning text/html. May I know how to set the response Content-Type so that the downloaded file is in excel format instead of html?

The code is here below:

public static function downloadFile(string $url, array $parameter) {
    $headers = ['Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=UTF-8'];
$client = new \GuzzleHttp\Client([
	'headers' => $headers
]);

$res = $client->post($url, [
	'form_params' => $parameter,
]);

$statusCode = $res->getStatusCode();
$content = $res->getBody()->getContents();

if($res->getStatusCode() != 200) {
	abort($res->getStatusCode());
}

return $content;
}

I am getting response.html file instead of a xlsx file. image

0 likes
0 replies

Please or to participate in this conversation.