8

I got desired response when i send cURL request from my PHP script.
My request is like this.

$data = array ("[product[name]]" => "nw",
               "[product[handle]]" => 150,
               "[product[interval_unit]]" => "day",
               "[product[interval]]" => 1,
               "[product[price_in_cents]]" => 0,
               "[product[initial_charge_in_cents]]" => 14200,
               "[product[return_url]]" =>"http://mytrial.com/office/selfie/themes/adcampaign/56cee935-185c-4dfs-asdfa-2b6b0ae84a4d",
               "[product[return_params]]" => "id={subscription_id}&customer_id={customer_id})");
$url="http://mytrial.com/office/selfie/themes/adcampaign/346423/products.json";
$ch  = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERPWD, 'sdfkjas2kjsd:x');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$res  = curl_exec($ch);
curl_close($ch);       

It's working properly. I want to do the same request in command line.First i json encoded the array and i tried with this commands

 curl -u sdfkjas2kjsd:x -H Accept:application/json -H Content-Type:application/json -x POST --data product[name]=nw&product[handle]=142&product[interval_unit]=day&product[interval]=1&product[price_in_cents]=0&product[initial_charge_in_cents]=14400&product[return_url]=http:\/\/54.145.218.63\/dev_lpad\/launchpad\/advertisers\/adcampaign\/56cee935-185c-4349-a8a1-2b6b0ae84a4d&product[return_params]={id={subscription_id}&customer_id={customer_id}}  http://mytrial.com/office/selfie/themes/adcampaign/346423/products.json  

Then i got the error.

Error: Unable to parse request body

Is there any way to solve this?

UPDATE : The URL provided here is a dummy value,Actually i am trying to connect with Chargify API (Recurring billing solution ).

ARUNBALAN NV
  • 1,634
  • 4
  • 17
  • 39

3 Answers3

6

It seems that your server does accept json payload post data. You probably forgot to json_decode your data, here is the fix:

curl -u sdfkjas2kjsd:x -H Accept:application/json -H Content-Type:application/json --data '{"product":{"name":"nw","handle":150,"interval_unit":"day","interval":1,"price_in_cents":0,"initial_charge_in_cents":14200,"return_url":"http:\/\/mytrial.com\/office\/selfie\/themes\/adcampaign\/56cee935-185c-4dfs-asdfa-2b6b0ae84a4d","return_params":"id={subscription_id}&customer_id={customer_id})"}}' http://mytrial.com/office/selfie/themes/adcampaign/346423/products.json

If i send it to my php script <?php var_dump(json_decode(file_get_contents('php://input'))); I see correct answer:

object(stdClass)#1 (1) {
   ["product"]=>
      object(stdClass)#2 (8) {
          ["name"]=> string(2) "nw"
          ["handle"]=> int(150)
  ...
shukshin.ivan
  • 11,075
  • 4
  • 53
  • 69
  • `{"errors":["Name: cannot be blank.","Interval unit: must be 'month' or 'day'.","Recurring Interval: cannot be blank.","Price: cannot be blank. Enter '0' if free."]}` this is the out – ARUNBALAN NV Mar 14 '16 at 11:14
  • Well, it looks like `curl` now sends correct json request, but there are some logic troubles. I can suppose that `[product[name]]` should be replaced with `product[name]` in the command. – shukshin.ivan Mar 14 '16 at 11:31
  • @ARUNBALANNV well, it is up to you, read docs of the service you submit data to. Guessing structure of a data needed is beyond the scope of your original question. – shukshin.ivan Mar 14 '16 at 14:09
  • My last try is to set the following structure of data: `{"product":{"name": ... "handle":...}}`. I've updated the answer – shukshin.ivan Mar 14 '16 at 14:18
  • `{"status":"500","error":"Internal Server Error"}` This is latest out – ARUNBALAN NV Mar 14 '16 at 16:51
  • @ARUNBALANNV that's definetely not curl issue. I just helped to substitute `parse error` with correct json. If I helped you a bit, please, upvote at least. – shukshin.ivan Mar 14 '16 at 19:26
4

Finally I could solve this issue by splitting the array parameters. My cURL cummand is this.

curl -u sdfkjas2kjsd:x -d  'product[name]":nw' -d '[product[handle]]=161' -d '[product[interval_unit]]=day' -d '[product[interval]]=1' -d '[product[price_in_cents]]=0' -d '[product[initial_charge_in_cents]]=14200' -d '[product[return_url]]=http:\/\/mytrial.com\/office\/selfie\/themes\/adcampaign\/56cee935-185c-4dfs-asdfa-2b6b0ae84a4d' -d 'product[return_params]=id={subscription_id}&{customer_id={customerC_id}})'  http://mytrial.com/office/selfie/themes/adcampaign/346423/products.json
ARUNBALAN NV
  • 1,634
  • 4
  • 17
  • 39
1

I think you should put your data inside single quotes

curl ... --data 'some data here' ...

EDIT:

ON WINDOWS the proper way to pass array argument via cURL is shown below:

curl -X POST http://localhost:8080/uploadMultipleFiles -H "content-type: multipart/form-data" -F "files=@C:\Users\...\Desktop\filename1.txt,C:\Users\...\Desktop\filename2.txt,C:\Users\...\Desktop\filename3.txt,C:\Users\...\Desktop\filename4.txt

See the use of comma separated filenames where the server expect files to be an Array of Files.

Saurabh Bhoomkar
  • 595
  • 1
  • 9
  • 29
Rinat
  • 429
  • 5
  • 14