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

maartenjean's avatar

Multiform with HTTP client

Hi all!

Moving from plain curl to the Laravel HTTP client I'm encountering an issue in the multiform. I'm trying to send this:

$payload  = [
            'data' => 'Belgium',
            'file' => new \CURLFILE(Storage::disk('local')->path('public/xxxx/xxxx.file'))
        ];

In plain curl it's working flawless, but when I try to use the attach() helper on the HTTP client, it doesn't allow me to send the complete $payload. Which is necessary for the endpoint I'm posting to.

Can someone give me a direction?

Curl code:

curl_setopt_array($curl, array(
            CURLOPT_URL => $path,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => '',
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 0,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => 'POST',
            CURLOPT_POSTFIELDS => $payload,
            CURLOPT_HTTPHEADER => array(
                'Accept: application/json, text/javascript, */*; q=0.01',
                'Accept-Language: nl,en-US;q=0.7,en;q=0.3',
                'Accept-Encoding: gzip, deflate, br',
                'Referer: ' . $this->base_url . '/login',
                'X-Requested-With: XMLHttpRequest',
                'Csrf-Token: ' . Crypt::decrypt($token),
                'Origin: $this->base_url',
                'Connection: keep-alive',
                'Cookie: ' . Crypt::decrypt($cookie),
            ),
        ));

        $response = curl_exec($curl);

        curl_close($curl);
0 likes
1 reply
vybeauregard's avatar

The HTTP client, while pretty slick, isn't a 1:1 conversion from curl. There is a separate method for handling attachments which will let you pass the name of the form element, the contents, and the filename it receives.

$file= fopen(Storage::disk('local')->path('public/xxxx/xxxx.file'), 'r');
 
$response = Http::attach('file', $file, 'photo.jpg')
			->post('http://example.com/attachments', ['data' = 'Belgium']);

https://laravel.com/docs/8.x/http-client#multi-part-requests

Please or to participate in this conversation.