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 ).

4
  • -H Content-Type:application/json Why did you say that? Commented Mar 10, 2016 at 10:04
  • because i am trying to send the json data Commented Mar 10, 2016 at 11:32
  • product[name]=nw&product[handle]… doesn't look remotely like JSON Commented Mar 10, 2016 at 11:52
  • So how can I resolve this? Commented Mar 13, 2016 at 6:32

3 Answers 3

6
+25

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)
  ...
Sign up to request clarification or add additional context in comments.

6 Comments

{"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
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.
@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.
My last try is to set the following structure of data: {"product":{"name": ... "handle":...}}. I've updated the answer
{"status":"500","error":"Internal Server Error"} This is latest out
|
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

Comments

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.

2 Comments

i have checked it. but no change
i have checked it. but no change

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.