Laravel HTTP client attach multiple files

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company

Mastering Multi-File Uploads with the Laravel HTTP Client: A Deep Dive

As developers building integrations with REST APIs, one of the most common tasks is handling complex data payloads, especially when dealing with file uploads. You are attempting to send multiple files via a multipart/form-data request, and while sending a single file is straightforward, iterating over an array of files often presents a hurdle.

If you’ve encountered issues trying to use the attach() method with an array of files in your Laravel HTTP Client calls, you are not alone. The documentation sometimes focuses on the basic usage, leaving advanced scenarios like bulk file attachments open to interpretation. As senior developers, we need robust, practical solutions.

This post will walk you through the correct, idiomatic way to handle attaching multiple files using the Laravel HTTP Client, ensuring your API interactions are seamless and efficient.

The Challenge: Attaching Multiple Files

When sending file data via HTTP, the request must be formatted as multipart/form-data. This format allows the client to send various parts of the request, including standard form fields and binary file contents. When you have an array of files (e.g., $files = $request->file('files')), simply passing this array structure into a single attach() call often doesn't work as expected because the underlying stream handling needs to be managed iteratively.

The core issue is that the HTTP client expects either a single file stream or a specific way to handle multiple streams simultaneously, which requires iteration.

The Solution: Iterating and Attaching Files Correctly

To successfully attach multiple files, you must loop through your array of uploaded files and call attach() for each file individually. This ensures that the HTTP client correctly processes each file as a distinct part of the multipart request.

Here is a practical example demonstrating how to achieve this:

use Illuminate\Support\Facades\Http;
use Illuminate\Http\Request;

class FileUploader
{
    public function uploadMultipleFiles(Request $request, string $apiUrl)
    {
        $token = env('API_KEY');
        $files = $request->file('document_attachments'); // Assume these are the files sent from the form

        if (empty($files)) {
            return response('No files provided.', 400);
        }

        // Initialize an array to hold all attachments for the request
        $attachments = [];

        foreach ($files as $file) {
            // Use the file object directly or stream it if necessary.
            // For simplicity with Laravel's Request objects, we often rely on 
            // reading the temporary file content.
            if ($file->isValid()) {
                $attachments[] = Http::withToken($token)
                    ->attach('files', $file->get()); // 'files' is the field name expected by the API
            }
        }

        // Make the POST request, passing the collected attachments
        $response = $attachments[0]->post($apiUrl, [
            'uuid' => $request->input('uuid'),
        ]);

        return $response;
    }
}

Explanation of Best Practices

  1. Iteration is Key: The critical step here is the foreach loop. Instead of trying to pass the entire array into one attach() call, we iterate over $files.
  2. File Object Handling: When dealing with files from a standard web request, you typically work with the uploaded file objects provided by the Request instance (like $file->get()). This method retrieves the raw content of the file as a string, which is then used as the stream data for attachment.
  3. Building Attachments: We collect the results of each attach() call into an array ($attachments). In this scenario, we are building multiple separate requests or ensuring that the final structure correctly bundles all file parts if the API supports it in a single request context (though the example above shows making sequential calls for clarity on attachment).

When designing interactions with external services, understanding how data streams are packaged is crucial. This aligns perfectly with the principles of robust API interaction we champion here at laravelcompany.com.

Conclusion

Handling multiple file uploads via the Laravel HTTP Client requires moving beyond a single attach() call and embracing iteration. By looping through your array of uploaded files and calling attach() for each one, you ensure that every file is correctly encoded into the multipart/form-data structure required by the receiving API. This approach is not only more reliable but also makes your code significantly easier to maintain and debug. Master this pattern, and your integration workflows will become much smoother!