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);
3
  • stackoverflow.com/questions/5104607/… Commented May 9, 2022 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? Commented May 9, 2022 at 15:34
  • Also you want 'item' not 'item[]'. Commented May 9, 2022 at 15:36

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.