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

ixudra's avatar

Sending file using Guzzle

I'm trying to transform a postman request into Guzzle. Postman works fine, Guzzle is a nightmare. Probably just a minor detail but I can't seem to figure it out. Any help is appreciated.

The postman request: Request

My request so far (not working)

            $response = $this->client->post(
                '/api/whitelabel/images', [
                    'headers' => [
                        'Accept'                => 'application/json',
                        'Content-Type'          => 'multipart/form-data',
                        'Authorization'         => 'Bearer '. $userToken,
                    ],
                    'multipart' => [
                        [
                            'name'     => 'myFile',
                            'contents' => file_get_contents($media->getPath()),
                        ],
                    ],
                ],
            );

Path to file is correct

0 likes
13 replies
lostdreamer_nl's avatar

Do you get any errors or exceptions?

What if you do:

        Try {
            $response = $this->client->post(
                '/api/whitelabel/images', [
                    'headers' => [
                        'Accept'                => 'application/json',
                        'Content-Type'          => 'multipart/form-data',
                        'Authorization'         => 'Bearer '. $userToken,
                    ],
                    'multipart' => [
                        [
                            'name'     => 'myFile',
                            'contents' => file_get_contents($media->getPath()),
                        ],
                    ],
                ],
            );
           echo $response->getBody()->getContents();
        } catch(\Exception $e) {
            echo $e->getMessage();
            $response = $e->getResponse();
            $responseBody = $response->getBody()->getContents();

            echo $responseBody;
            exit;
        }
ixudra's avatar

@lostdreamer_nl I get a guzzle client error with a 400 status, saying No image found. For some reason the data is not submitted correctly, or at least the server is not finding it. Not sure how, since I got this code from the official docs

lostdreamer_nl's avatar

What if you also add the filename?


                    'multipart' => [
                        [
                            'name'     => 'myFile',
                            'contents' => file_get_contents($media->getPath()),
                            'filename' => 'screenshot.png'
                        ]

Also: Could you check (composer.lock file) which version of Guzzle is installed?

1 like
lostdreamer_nl's avatar

I see a lot of documentation stating to use:

                'contents'      => fopen($media->getPath(), 'r'),

instead of file_get_contents. it might expect a stream instead of a string.

ixudra's avatar

@lostdreamer_nl Tried that as well, didn't work

Sorry, I know you're trying to help but I've been stuck here for a while...

ixudra's avatar

@lostdreamer_nl Got some feedback from that, but not sure what to make of it though:

Host: example.com
Accept: application/json
Content-Type: multipart/form-data
Authorization: Bearer token-xyz
Content-Length: 78514
User-Agent:  GuzzleHttp/6.3.3 curl/7.47.0 PHP/7.2.5-1+ubuntu16.04.1+deb.sury.org+1

--ed71df491f8e9775c5c535bc327bfb639a13ce11
Content-Disposition: form-data; name="myFile"; filename="plate.jpg"
Content-Length: 78188
Content-Type: image/jpeg

 Ï Ó\x00\x10JFIF\x00\x01\x01\x01\x01,\x01,\x00\x00 Ý\x00,Photoshop 3.0\x0 .... 
--ed71df491f8e9775c5c535bc327bfb639a13ce11--
ixudra's avatar

This is the request that Postman is sending:

POST /api/whitelabel/images HTTP/1.1
Host: example.com
Accept: application/json
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW
Authorization: Bearer token-xyz
Cache-Control: no-cache

------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="myFile"; filename="plate.jpg"
Content-Type: image/jpeg


------WebKitFormBoundary7MA4YWxkTrZu0gW--
lostdreamer_nl's avatar
Level 53

Looking at the missing boundary I found this topic:

https://github.com/guzzle/guzzle/issues/1885

There they said they got it fixed by removing the content-type header they were setting themselves.

How does this work:?

        Try {
            $response = $this->client->post(
                '/api/whitelabel/images', [
                    'headers' => [
                        'Accept'                => 'application/json',
//                        'Content-Type'          => 'multipart/form-data',  // <-- commented out this line
                        'Authorization'         => 'Bearer '. $userToken,
                    ],
                    'multipart' => [
                        [
                            'name'     => 'myFile',
                            'contents' => file_get_contents($media->getPath()),
                        ],
                    ],
                ],
            );
           echo $response->getBody()->getContents();
        } catch(\Exception $e) {
            echo $e->getMessage();
            $response = $e->getResponse();
            $responseBody = $response->getBody()->getContents();

            echo $responseBody;
            exit;
        }
2 likes
ixudra's avatar

I feel like slamming my head against my desk for at least 15 mins but my desk is actually pretty expensive... Thanks for your patience and your assist @lostdreamer_nl , this did indeed fix my issue.

lostdreamer_nl's avatar

n/p , for me it's a way to get a better understanding of different packages as well ;)

Good luck with the project

marcellopato's avatar

Why I´m getting this error:

Symfony \ Component \ Debug \ Exception \ FatalThrowableError (E_ERROR)
Call to a member function getPath() on string

If a had Guzzle installed?

Please or to participate in this conversation.