arrowill12
arrowill12

Reputation: 1804

Guzzle send empty array in multipart request

I am using Guzzle 6 to send a multipart form request to a 3rd party api(cloud Foundry). the api takes 2 parameters "resources" and "application". here is the doc for the call I am making. in short this deploys a binary to an an applications server. below is the code I am using in Guzzle. I am getting an "invalid resource type" error when trying to send an empty array as the contents of the "resource" parameter. Guzzle seems to only allow strings here? the api requires that an empty array be sent when a new binary is being pushed.

here is the code:

 $response = $this->client->put($this->baseUrl . "apps/7887990-654e-4516-8ce9-b37bc2f93a87/bits", [
         'multipart' => [
             [
                 'name' => 'resources',
                 'contents' => []
             ],
             [
                 'name' => 'application',
                 'contents' => '@/tmp/cfdownloadYQfOp7',
             ]
         ]
     ]);

the above fails with the mentioned error, and changing ti to a string results in a bad request to the api.

here is the curl command that works properly:

curl -k -X PUT -H "Authorization:token here" -F 'resources=[]' -F "application=@/tmp/cfdownloadF9AxlE" https://api.cloudfoundry.com/v2/apps/2d0f491b-d8dd-4b3a-96f9-58b3678e5dad/bits

does anyone know how to get this to work using the above guzzle code?

Upvotes: 1

Views: 3352

Answers (1)

arrowill12
arrowill12

Reputation: 1804

I have solved this issue. turns out that it was not am a matter of sending an array, but another error being thrown by guzzle that was masking the real issue.

first I set guzzle to debug, and turned off exceptions(see below). Guzzle will mask the actual exceptions from the third party if this is not off. I was getting 400 bad response which is correct but it was hiding the actual message which was that the file I was trying to send was not able to be unzipped. I then decided to change the multipart request to use the 'fopen' option from the guzzle docs, rather than using '@' from the cloudfoundry docs. This solved the problem and all is working fine now. see belwo for the updated request.

new Client(['debug'=>true,'exceptions'=>false,'headers' => ['Authorization' => "Bearer " . $token, "Accept" => "application/json"], 'verify' => false])

guzzle request:

  $response = $this->client->put($this->baseUrl . "apps/cb44bb975-654e-4516-8ce9-b37bc2f93a87/bits", [
         'multipart' => [
             [
                 'name' => 'resources',
                 'contents' => '[]'
             ],
             [
                 'name' => 'application',
                 'contents' => fopen('/tmp/cfdownloadYQfOp7', 'r')
             ]
         ]
     ]);

Upvotes: 2

Related Questions