Ethen
Ethen

Reputation: 171

sending an array through cURL POST

Hi i am testing a scenario for REST web service for mobile app.During testing i need to send an array to my php programme using post method. which i am doing through cURL console. rest of the thing is working fine except passing an array. Please suggest any changes.

following code i am passing in cURL console

C:\curlw32>curl -H "Content-Type: application/json" -X POST http://localhost/slim-login/api/submit -d "{\"specialtyCheckbox\":\"[1,2,3]\"}"

and here is the php code for catching it

$request = Slim::getInstance()->request();
$onsubmit_content = json_decode($request->getBody());
$spec=$onsubmit_content->specialtyCheckbox;
echo json_encode(count($spec));

Here the length of the array it is showing 1.

Upvotes: 2

Views: 12618

Answers (4)

Mahmoud Magdy
Mahmoud Magdy

Reputation: 923

Please try This

 curl -d '{"previous_questions":"['hello', 'world', 'finally']"}' -H "Content-Type: application/json" -X POST http://localhost:5000/quizzes

Upvotes: 0

George Mathew
George Mathew

Reputation: 61

If you wanna pass the array over the CURL call the answer is very simple. Put all the input parameters in array like-

$post = array('prgmCode'=>'3',
                'userStateCode'=>'TA',
                );

If you need to pass an input array on this you can set

`$chkBoxArr = array(1,2,3,4);
$post = array('prgmCode'=>'3',
                'userStateCode'=>'TA',
                'chkBoxArr'=>$chkBoxArr
                );`

You can use function http_build_query() to make a query string from the array.

`$data = http_build_query($post);`

And set the data to your curl

`curl_setopt($curlSession, CURLOPT_POSTFIELDS, $data);`

Check the request in the server side. Done. :) :)

Upvotes: 0

sandy
sandy

Reputation: 1158

Can you try passing your array through a PHP script( using CURL)

$ch = curl_init ($url); // your URL to send array data
curl_setopt ($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData); // Your array field
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec ($ch);

print_r($result);

Upvotes: 0

mpm
mpm

Reputation: 20155

dont use quotes around your array (if you want to send a json array ):

"{\"specialtyCheckbox\":[1,2,3]}"

Upvotes: 2

Related Questions