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

laracoft's avatar

PHP equivalent of cURL

This command lets me upload a file to my Laravel controller using cURL and I was able to get the file in Laravel using $request->file("files")

curl \
    -F "action=notify" \
    -F "files[][email protected]" \
    --fail-with-body \
    https://example.com/upload

What is the equivalent in Laravel? I have tried both asForm and asMultipart and did not work, the controller was executed, but $request->file("files") had no files

$url = "https://example.com/upload";
$path = storage_path("text-file.txt");
$response = Http::withHeaders([])
    ->asForm()
    // ->asMultipart()
    ->attach('files[]', File::get($path), File::basename($path))
    ->post($url, [
        [
            'name' => 'action',
            'contents' => 'notify',
        ],
    ]);
0 likes
2 replies
tykus's avatar

All should be okay, except you need to remove the asForm method call - it replaces the body format in the Http Request which was previously set as multipart/form-data when you call the attach() method.

$url = "https://example.com/upload";
$path = storage_path("text-file.txt");
$response = Http::withHeaders([])
    ->attach('files[]', File::get($path), File::basename($path))
    ->post($url, [
        [
            'name' => 'action',
            'contents' => 'notify',
        ],
    ]);
1 like

Please or to participate in this conversation.