Upload file using Guzzle Upload file using Guzzle curl curl

Upload file using Guzzle


I'd review the Guzzle's multipart request options. I see two issues:

  1. The JSON data needs to be stringified and passed with the same name you're using in the curl request (it's confusingly named body).
  2. The type in the curl request maps to the header Content-Type. From $ man curl:

    You can also tell curl what Content-Type to use by using 'type='.

Try something like:

$response = $client->request('POST', 'http://mydomain.de:8080/spots', [    'multipart' => [        [            'name'     => 'body',            'contents' => json_encode(['name' => 'Test', 'country' => 'Deutschland']),            'headers'  => ['Content-Type' => 'application/json']        ],        [            'name'     => 'file',            'contents' => fopen('617103.mp4', 'r'),            'headers'  => ['Content-Type' => 'video/mp4']        ],    ],]);


  • While using the multipart option, make sure you are not passing content-type => application/json :)
  • If you want to POST form fields AND upload file together, just use this multipart option. It's an array of arrays where name is form field name and it's value is the POSTed form value. An example:
'multipart' => [                [                    'name' => 'attachments[]', // could also be `file` array                    'contents' => $attachment->getContent(),                    'filename' => 'dummy.png',                ],                [                    'name' => 'username',                    'contents' => $username                ]            ]