0

I have searched a lot, and found many related questions and forums related to this, but this one is a challenging one.

I'm trying to POST a complex array via curl. It has to be form-data while the first value in the array is of type JSON. The two other values of array are two images which are uploaded and ready to send.

I tried to run it in Postman, and works perfectly fine. I used the generated PHP code from Postman, but it is not working. Seems like postman is handling some of its tricks without revealing them to us.

Any way, I'm posting a Postman image to illustrate what I mean: enter image description here

As you can see, I'm sending the data in form-data tab, my first value (param1) is a JSON with content-type application/json, the second and third values are images uploaded in Postman.

This works just fine in Postman.

The problem is, if I set Content-Type:multipart/form-data in header, the destination server throws an error saying the content-type must be JSON.

If I set the Content-Type:application/json in header, the destination server says content must be of type Multipart.

Somehow, I need to set both content-types. The main one as form-data and the one for param1 as JSON.

I paste the Postman code as well, may that be a good start for you fellas to help out with the code.

Postman Code:


$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => 'http://xxxxx.com/xxxx/xxx/xxxx',
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => '',
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => 'POST',
  CURLOPT_POSTFIELDS => array('param1' => '{
   "AgentId":"1414",
   "ContractId":36529,
   "Files":[
      {
         "FileName":"car_card_front_image.png",
         "FileTypeId":2
      },
      {
         "FileName":"car_card_back_image.png",
         "FileTypeId":2
      }
   ]
}','param2'=> new CURLFILE('/C:/images/icons/car_card_back_image.png'),'param3'=> new CURLFILE('/C:/images/icons/car_card_front_image.png')),
  CURLOPT_HTTPHEADER => array(
    'authenticationToken: xxxx-xxx-xx-xxxxxxxx'
  ),
));

$response = curl_exec($curl);

curl_close($curl);
echo $response;

The PHP generated code by postman, is not working. One of the reasons can be that there's no content-type mentioned in it.

I tried modifying the code, adding content-types in header and in parameter, nothing seems to work.

If Postman can do it, we should be able it too, right?

Go ahead, make as much changes as you would or suggest anything that comes to your mind, I will test them all.

Cheeeeers...

Sean
  • 953
  • 1
  • 7
  • 26

1 Answers1

0

May i suggest the ixudrra/curl library ?

It would make your life easier ....

$response = Curl::to('http://example.org')
        ->withData( array( 'Foo' => 'Bar' ) )
        ->withFile( 'image_1', '/path/to/dir/image1.png', 'image/png', 'imageName1.png' )
        ->withFile( 'image_2', '/path/to/dir/image2.png', 'image/png', 'imageName2.png' )
        ->post();