It should work but try adding ->asForm()
Are you getting any errors?
Did you remember to disable csrf for the url?
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
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!
Please or to participate in this conversation.