Sending command vi API gives 400 error even everything is same as web panel

Md Azharuddin7 years ago

I am trying to send command via API using PHP cUrl
I have everything same as web request but still not working and i get http_code =400
Bad request

URL=> http://my_email:my_password@my_site:8082/api/commands/send

Post Data => { "attributes": "{}", "description": "New...", "deviceId": "84", "id": 0, "textChannel": 0, "type": "engineStop" }

here is my curl code

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);	

curl_setopt($ch, CURLOPT_POST, 1);


curl_setopt($ch, CURLOPT_POSTFIELDS,$strPost);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                                            'Content-Type: application/json',
                                            'Connection: Keep-Alive'
                                            ));           


$response   = curl_exec($ch);     
$http_code  = curl_getinfo($ch, CURLINFO_HTTP_CODE);
echo $http_code  ;
curl_close($ch);
Anton Tananaev7 years ago

One problem I can see straight away is that your deviceId field is a string, not integer. I would recommend you to compare request with what official web app sends.

Md Azharuddin7 years ago

I have tried both String and integer but not worked
I have compared all these with official web app parameters and its same still it is giving 400 don't know where the problem is
all other APIs like Geofence Events are working fine but this one causing error

Anton Tananaev7 years ago

If request is exactly the same, it should obviously work exactly like with the official web app. You must be missing something.

Md Azharuddin7 years ago

OH, I have found the error
it was in attribute field it was sending "{}" instead of {} i have removed extra quotes and now it is giving me http_code 202 code
and response like this

{"id":0,"attributes":{},"deviceId":84,"type":"engineStop","textChannel":false,"description":"New…"}
Anton Tananaev7 years ago

It means that it works.

Md Azharuddin7 years ago

thank you Sir.