2

I'm trying to POST multipart and json data with Guzzle to build my apps with Phonegap Build API. I've tried many adjustment but still got error results. Here's the latest function I'm using:

public function testBuild(Request $request)
{
     $zip_path = storage_path('zip/testing.zip');
     $upload = $this->client->request('POST', 'apps',
          ['json' =>
            ['data' => array(
              'title'         => $request->title,
              'create_method' => 'file',
              'share'         => 'true',
              'private'       => 'false',
            )],
           'multipart' => 
            ['name'           => 'file',
             'contents'       => fopen($zip_path, 'r')
            ]
          ]);
      $result = $upload->getBody();
      return $result;
}

This is my the correct curl format that has success result from the API, but with file I have in my desktop:

curl -F file=@/Users/dedenbangkit/Desktop/testing.zip 
-u email@email.com 
-F 'data={"title":"API V1 App","version":"0.1.0","create_method":"file"}'
 https://build.phonegap.com/api/v1/apps
Deden Bangkit
  • 598
  • 1
  • 5
  • 19

1 Answers1

5

As mentioned before, you cannot use multipart and json together.

In your curl example it's just a multipart form, so use the same in Guzzle:

$this->client->request('POST', 'apps', [
    'multipart' => [
        [
            'name' => 'file',
            'contents' => fopen($zip_path, 'r'),
        ],
        [
            'name' => 'data',
            'contents' => json_encode(
                [
                    'title' => $request->title,
                    'create_method' => 'file',
                    'share' => 'true',
                    'private' => 'false',
                ]
            ),
        ]
    ]
]);
Alexey Shokov
  • 4,775
  • 1
  • 21
  • 22