Search code examples
phplaravelguzzle

Guzzle Not Sending Headers From Form MultiPart


I am using "guzzlehttp/guzzle": "~6.0", and trying to post a file to an API endpoint. The file posts fine when using RequestBin but the API is not getting the header it requires. The Header is not sent to Request bin either. According to the docs, I need to do an array of associative arrays. http://docs.guzzlephp.org/en/latest/quickstart.html#post-form-requests

However, this is not working. Here's the Guzzle request:

$client = new GuzzleHttp\Client(['base_uri' => '127.0.0.1:3000']);
        $response = $client->request('POST', '/process', [
            'multipart' => [
                [
                    'name'     => 'file',
                    'contents' => $file,
                    'bucketName' => 'test',
                    'headers'  => ['X-API-Key' => 'abc345']
                ],
            ]
        ]);

What am I doing wrong that it's not sending the header?

Thank you very much,

Josh


Solution

  • Headers is an $option, that's mean it must be at the same level as multipart.

    <?php
          $response = $client->request('POST', '/process', [
            'multipart' => [
                [
                    'name' => 'file',
                    'contents' => 'test',
                    'bucketName' => 'test',
                ],
            ],
            'headers' => ['X-API-Key' => 'abc345'] // <------- HERE
        ]);