Thursday, March 30, 2017

How to download or view word doc from a rest api using Guzzle

I am pretty new to using Guzzle. 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?

Thanks in advance!



via Joren vh

Advertisement