Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

iKyzu's avatar
Level 1

HTTP Client File Download

Hello,

I am using HTTP Client to try and download file from external server via api. I am using get methong with some basicAuth to connect and fetch the file that I want.

In the browser when I lunch the URL eveything is fine, the file starts to download but I can't seem to get it work inside the application.

I am getting errors like:

Symfony \ Component \ HttpFoundation \ File \ Exception \ FileNotFoundException

or

Argument #1 must not contain any null bytes (when I do dump(strlen($file)) there seems to be data)

How can I make this work? Best Regards!

0 likes
16 replies
iKyzu's avatar
Level 1
$file = Http::withBasicAuth(username, password)->withHeaders([
			'accept' => 'application/octet-stream',
		])->get('http://mydomain.com/Download?docId='.$id);

return response()->download($file);

There is not much really to the code

Sinnbeck's avatar

@iKyzu did you try just saving it to disk? As a test

$response = Http::withBasicAuth(username, password)->withHeaders([
			'accept' => 'application/octet-stream',
		])->get('http://mydomain.com/Download?docId='.$id);

Storage::putFile('downloads', $response->body());

Or try a streamed download

return response()->streamDownload(function () {
       $response = Http::withBasicAuth(username, password)->withHeaders([
			'accept' => 'application/octet-stream',
		])->get('http://mydomain.com/Download?docId='.$id);
    echo $response->body();
}, 'filename.pdf'); // replace with actual name
iKyzu's avatar
Level 1

@Sinnbeck

When I try to save it in a disk I am getting: Symfony \ Component \ HttpFoundation \ File \ Exception \ FileNotFoundException

Stream download - download a file but it's empty/damaged (0 bytes)

Sinnbeck's avatar

@iKyzu are you able to download any other file using this technique? From a site without auth

iKyzu's avatar
Level 1

@Sinnbeck yes I am able to download other file using this technique.

I see I need to find what's wrong with the place I am trying to get the file from.

iKyzu's avatar
Level 1

@Sinnbeck I found a way to work:

$tempFile = tempnam(sys_get_temp_dir(), $fileName);
copy('http://localhost/Download?fileId='.$id, $tempFile);

return response()->download($tempFile, $fileName);
brianthomas's avatar

@Sinnbeck Hi, i tried all your code . But i get the error below. is_file() expects parameter 1 to be a valid path, string given

1 like
Snapey's avatar

when it says mydomain.com, is it another webserver? You are not trying to load from this same site (as a test)?

iKyzu's avatar
Level 1

@Snapey With mydomain.com I mean another webserver since the file should be downloaded from the server.

vinalvess's avatar

I had issues recently download a .pdf blob from my report server and this is what solved for me. Just a little context, my report server (.Net Core app) is another docker container running on the same docker network as my Laravel app container and the report server is not exposed to the internet, the reason with I used the withoutVerifying() method.

...
$myUrl = 'url path to the file';
$report = ReportResource::find($id);
$tempFile = tempnam(sys_get_temp_dir(), $fNameTemp);
Http::withoutVerifying()->send('GET', $myUrl, ['sink' => $tempFile]);
return response()->download($tempFile, $report->name.'.'.$report->export_type);
jwhoami's avatar

With laravel 11, this worked for me

Storage::put("file.pdf", $response->body());

The filename can be generated dynamically though.

Please or to participate in this conversation.