Agent http statuscode "does not exist"

I’m using my agent to call a php file.

I run the code below, I get an error: “the index ‘statuscode’ does not exist.”

`function sendData(){
server.log(“in sendData”);

local mysqlURL = "http://162.216.17.33/ElectricImp/phptest.php"

local getreq = http.get(mysqlURL);
local res = getreq.sendsync;
server.log(res.statuscode);    

}
`

The php code is simply:
`<?php

echo("This works");

?>
`

and “This works” shows when I go to the page in my browser.

Any ideas on what I’m doing wrong? Thanks in advance. :slight_smile:

You forgot the () at the end of sendsynch.

In your code, when you try logging res.statuscode, res is actually a pointer to the sendsync function instead of the result of the function.

`function sendData(){
server.log(“in sendData”);

local mysqlURL = "http://162.216.17.33/ElectricImp/phptest.php&quot;

local getreq = http.get(mysqlURL);
local res = getreq.sendsync();
server.log(res.statuscode);    

}`

Oh, okay. Thanks for both the correction AND the explanation.
It works perfectly now! :smiley:

@beardedinventor

Does sendsync execute the full script?

My php code should be changing a database. But when I call it with my Agent code (I get a 200 response), nothing happens.

I server.logged the URL my Agent code calls… And when I manually put that into a browser, the database is changed.

request.sendsynch() sends the request, and blocks until it gets a response (ie - no other code executes until it gets a response, or times out).

A 200 response typically means that the request was successful. It’s odd that calling the request from a browser works (or ‘fully’ executes), but calling it from an agent does not.

It sounds like something on your sever, although it’s difficult to say what without knowing more about your php code.

What do you get if you log the responses body?

server.log(res.body);

Wow. So that was useful.
When I call “server.log(res.body);”, I get:

Error message: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' , , )' at line 1

When I call my url:
local mysqlURL = "http://162.216.17.33/ElectricImp/FromImptoSQL.php?col1=" + value1 + "&col2=" + value2 +" &col3=" + value3 + "&col4=" + value4 + "&col5=" + value5 + "&col6=" + value6

server.log(mysqlURL) prints the appropriate url:

http://162.216.17.33/ElectricImp/FromImptoSQL.php?col1=1&col2=1 &col3=1&col4=1&col5=1&col6=1

But the values that are getting passed to my php aren’t right.
In my php, the values get assigned:
`<?php

//Get column variables
$column1 = $_REQUEST[“col1”];
echo(“column1”);
echo($column1);
$column2 = $_REQUEST[“col2”];
echo(“column2”);
echo($column2);
$column3 = $_REQUEST[“col3”];
echo(“column3”);
echo($column3);
$column4 = $_REQUEST[“col4”];
echo(“column4”);
echo($column4);
$column5 = $_REQUEST[“col5”];
echo(“column5”);
echo($column5);
$column6 = $_REQUEST[“col6”];
echo(“column6”);
echo($column6);
`

But the output is:

column1 1 column2 1 column3 column4 column5 column6

And now, after posting, I see the space.

It all works now. Thank you.

It looks like you have a space in your request string after value2:

... "&col2=" + value2 + " &col3"

Rather than manually build request strings, we generally encourage people to use the http.urlencode function, which takes a table, and returns a string:

local baseUrl = "http://162.216.17.33/ElectricImp/FromImptoSQL.php"; local query = http.urlencode { col1=value1, col2=value2, col3=value3, col4=value4, col5=value5, col6=value6 }); local requestUrl = request + "?" + query; ...