0

I need to do a POST request with cURL to send an arbitrary number of strings under a given key ("item[]").

As an example, here is an equivalent POST through an HTTP form. This one works as expected:

<form method="post" action="{url}" enctype="multipart/form-data">
    <input type="text" name="item[]" value="">
    <input type="text" name="item[]" value="">
    <input type="text" name="item[]" value="">
    <input type="text" name="id" value="someid">
</form>

I have tried building the request like this (see below), but to no avail. The receiving server receives value "Array" for "item[]" instead of 3 distinct strings.

$data [
   'item[]' => ['item1', 'item2', 'item3'],
   'id' => 'someid'
];
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
David
  • 1,898
  • 2
  • 14
  • 32
  • https://stackoverflow.com/questions/5104607/how-to-include-array-data-in-curlopt-postfields/5104620#5104620 – Lawrence Cherone May 09 '22 at 15:26
  • This example seems to do a `application/x-www-form-urlencoded` POST but I need to do a `multipart/form-data` POST (because in some cases a binary file has to be attached). How can I do that? – David May 09 '22 at 15:34
  • Also you want `'item'` not `'item[]'`. – AbraCadaver May 09 '22 at 15:36

0 Answers0