How to use the Clickable guzzle to redirect a file download of a request

Asked

Viewed 38 times

0

I need your help to find a way to be a 'proxy' between the frontend and a third-party software component using Laravel.

Third-party software is on my company’s network and generates the files on demand. For example, if I click http://mycompany-third-party.com/12/csv, it will read the database data and deliver it for download as a response (in this case, a csv file).

But I cannot allow users to reach the third party endpoint directly, so I want to use the Laravel (guzzle) to request the file to a third party and then redirect it to the specific client without storing it on my local disk.

How can I do something like this using Laravel / guzzle or another library?

This is the way I tried

$response = Http::withOptions(['stream' => true])->withHeaders([
            'X-Metabase-Session' => $this->token,
       ])->post('http://mycompany-third-party.com/12/csv');

        $headers = $response->headers();

        return response($response->getBody())->withHeaders($headers);

1 answer

0

I found the solution. We have to use the Streamed Downloads.

You’re here for whoever needs you.


        $response = Http::withOptions(['stream' => true])->withHeaders([
            'X-Metabase-Session' => $this->token,
        ])->post(your-3th-url);


        return response()->streamDownload(function () use ($response) {
            echo $response->getBody()->getContents();
        }, 'fileName.extType');

See you around.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.