Q:

How to download word document from API using Laravel

I am pretty new to using Guzzle with Laravel. I currently just use it for communication between my front-end and a seperate REST api.
I'm trying to download a file from my api but I'm not sure how to go about it. I could just specify the path to the file and directly download it but I also want to be able to stream or view it in my browser so a user can just view a word document instead of just downloading it.
Currently I'm sending a GET request from front end project (with backend to do api calls) to the api project: 
$resp = $client->request('GET', env('API_BASE_URL').'/project/'.$id. '/download-report', [ 'headers' => [ 'Authorization' => 'Bearer '. session()->get('api_token') ] ]);

and in my api backend I return the file with the ->download() function. 
return response()->download($report->getPath());

Can someone explain what would be the best way to approach this situation?
Solutions for both issues would be awesome, just downloading it, or actually viewing the contents of the word document.
Thanks in advance!

A:

First of all, it's better to serve files with the web server (and leave PHP for "smarter" work). The idea behind it is to generate a "secure" link, that hides the real file and has an expiration timeout.
There are a few ways to do that (depends on your server: nginx, Apache or something else). A good example for nginx, with which you can just generate such link in your API, and send it to the end user through your frontend.
If you prefer to do in PHP for some reasons, just download the file to a temporary file and send it using response()->download() with corresponding headers (Content-Type) in your frontend.
$tmpFile = tempnam(sys_get_temp_dir(), 'reports_');

$resp = $client->request('GET', '...', [
    // ... your options ...
    'sink' => $tmpFile,
]);

response()->download(
    $tmpFile,
    null,
    [
        'Content-Type' => $resp->getHeader('Content-Type')[0]
    ]
)->deleteFileAfterSend(true);

