Agent doesn't like my incoming PHP JSON

I want to send JSON data to the agent from my PHP script.
The agent doesn’t like the JSON and says “Invalid JSON String”.

Here is my agent:

// AGENT IN Receiving Data from Website
// ----------------------------------------------------
http.onrequest(function(request, response){
    // decode the json - if it's invalid json an error will be thrown
    local incoming = http.jsondecode(request.body);
	server.log("Incoming Data: " +;

     // send response to whoever hit the agent url
    response.send(200, "OK");
  catch (e) {
    // send a response indicating invalid JSON
    response.send(500, "Invalid JSON string");

And this is my PHP script for testing:


$data = array("mykey" => "CP376aIe4MeSTwG", "relay1" => 1, "relay2" => 0);                                                                    
$data_string = json_encode($data);
$ch = curl_init('');                                                                      
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);                                                                  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                      
curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
    'Content-Type: application/json',                                                                                
    'Content-Length: ' . strlen($data_string))                                                                       

$result = curl_exec($ch);
echo $result;


Does anyone have more information about this?
My PHP script can POST to other PHP scripts and everything is valid JSON.
But a POST to the agent … the agent doesn’t like it.

I don’t know PHP at all, but it looks to me like you are referencing an index (.data) that isn’t there.

Instead of : server.log("Incoming Data: " +;

Try: server.log("Incoming Data: " + http.jsonencode(incoming));

Even if I comment-out the server log line, it still sends the non valid response.

Tack the exception onto the end of your response so you can see what you’re dealing with, eg:

response.send(500, "Invalid JSON string: "+e);

That was a great idea!
With that I found my problem.
I had an extra line that I didn’t comment-out properly, thus threw an error.