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

Demers94's avatar

Sending uploaded file to external API using the HTTP client

Hello,

I'm letting users upload a file through a form. I want to use the uploaded file and send it to an external API. Here is the code I'm using, taking advantage of Laravel's HTTP client :

$request->validate([
    'file' => ['file', 'mimes:docx,pdf,png,jpg']
]);

Http::withToken(...)
    ->attach('file', $request->file('file'))
    ->post('https://my-api.com/files');

The code above doesn't work, I can't retrieve the uploaded file on the API's end using $request->file('file').

I also tried using fopen() like the documentation suggests :

$file = fopen($file->getRealPath(), 'r');
Http::withToken(...)
    ->attach('file', $request->file('file'))
    ->post('https://my-api.com/files');

But this doesn't work either.

However, it does work if I use a public file instead of the user uploaded one :

$file = fopen(public_path('/images/logo.png'), 'r');
Http::withToken(...)
    ->attach('file', $request->file('file'))
    ->post('https://my-api.com/files');

How can I send the uploaded file instead?

I tried saving it on disk first like so :

$path = $file->store('uploads');
$file = fopen(storage_path('app/' . $path), 'r');
Http::withToken(...)
    ->attach('file', $request->file('file'))
    ->post('https://my-api.com/files');

But that doesn't work either.

I'm not sure what else to try, does anyone know the solution to this?

Thanks for the help!

0 likes
5 replies
Sinnbeck's avatar

It should work but try adding ->asForm()

Are you getting any errors?

Did you remember to disable csrf for the url?

1 like
martinbean's avatar

@demers94 Why are you having users upload your file, just to then wait for you to re-upload it to a completely different location? You’re making the user waiting twice the length of time, then. Because now they have to wait for the upload to your server to complete, and then the transfer from your server to this other API, before they get a response.

1 like
Demers94's avatar

@sinnbeck That didn't work, the attach() method already calls asMultipart() under the hood

No, I'm not getting any errors (the file is simply missing from the request on the API's end so I get a validation error when I try to check if a file exists). And yes CSRF is disabled 🤔

@martinbean That's ... a really good point. I was converting legacy code so I just replaced the actual file storage with an API call, but it would make a lot more sense to send it to the API directly. I'll try that instead 👍

Still, I'd like to figure out why it doesn't work and how I can fix it. Maybe in the future there will be a need to upload it at both places, even though for now it's redundant and just making things slower.

I appreciate the help!

ImNoob's avatar

@demers94 hi..i also face the same problem as u..but as for my case i have found the solution by adding Headers for Content-Type..hope this can help u

$request->validate([ 'file' => ['file', 'mimes:docx,pdf,png,jpg'] ]);

Http::withHeaders([ 'Content-Type' => 'multipart/form-data' ]) ->withToken(...) ->attach('file', $request->file('file')) ->post('your external api link');

Lovelogist's avatar

This is what works for me, when I used it with zip file upload.

$zipFileName = 'public/memes/' . Str::random(11) . ".zip";
$zipFilePath = Storage::path($zipFileName);
// e.g. C:/laragon/www/laravel-project/storage/app/public/memes/xl26SNa6p3h.zip

Http::withBody(file_get_contents($zipFilePath), 'application/zip')
->withToken($token)
->post($url);

Please or to participate in this conversation.