0
Answered

Custom HTTP Driver - Receiving a response

r riksma 10 years ago in iRidium Script / AV and Driver scripts updated 10 years ago 16
Good afternoon,

I've been working on creating a custom HTTP driver. I can send commands without any problems but I would like to parse the returned response code and data as well.

I have been unable to find documentation on how to do this. Could you provide me with an example or tell me what's wrong with the following code? It does send the commands but the response from the server is lost.

function HTTPDriver(host,port){

this.init = function() {
this.HTTPDriver = IR.CreateDevice(IR.DEVICE_CUSTOM_HTTP_TCP, "HTTPDriver", host, port, "root", "atata", IR.SSL_OFF, IR.BACKGROUND_OFF);
};

this.get = function(param) {
IR.Log("Param " + param);
return this.HTTPDriver.Send(['GET,' + param + ',']);
};
}

IR.AddListener(IR.EVENT_START, 0, function () {
var httpDriver = new HTTPDriver("testserver","80");
httpDriver.init();

IR.AddListener(IR.EVENT_RECEIVE_TEXT, httpDriver, function(text)
{
IR.Log("RECEIVING TEXT");
IR.Log(text);

})

IR.AddListener(IR.EVENT_RECEIVE_DATA, httpDriver, function(text)
{
IR.Log("RECEIVING DATA");
IR.Log(text);

})

IR.Log(httpDriver.get("/abc/lol?123"));
httpDriver.get("/diag")
IR.Log("DONE!");
});

Answer

Answer
Answered
status can not be obtained from the server via HTTP driver
Hi, R riksma!

Please move the subscriptions to EVENT_RECEIVE_TEXT and EVENT_RECEIVE_DATA into the HTTPDriver constructor and change the event owner to this.HTTPDriver.

For example:
function HTTPDriver(host,port){
// ..
IR.AddListener(IR.EVENT_RECEIVE_TEXT, this.HTTPDriver, function(text)
{ // Parser
});
//..
};

Hi Sergey,

That worked, thank you!
Is it correct that POST does not return data to IR.EVENT_RECEIVE_TEXT ?

When using GET i do get a response, but only the content.
Is it possible to receive the HTTP status code somehow? (like a 200 OK or 404 page not found)
To get the full information comes from the construct.
function HTTPDriver(host,port){    // ..
    
    IR.AddListener(IR.EVENT_RECEIVE_TEXT, this.HTTPDriver, function(text)
    {
       buffer += text;
    });
    
   //Parse function should use in OFFLINE Listener

   IR.AddLstener(IR.EVENT_OFFLINE, this.HTTPDriver, function() 
    {
          //Parse buffer
    });
//.. };
I think I understand what you mean, I should wait untill all data is in.
this is the code I use now:

var buffer = "";
IR.AddListener(IR.EVENT_RECEIVE_TEXT, this.HttpModbus, function(text)
{
buffer += text;
IR.Log("Receive text: " + text);
IR.Log("Receive!");
});
IR.AddListener(IR.EVENT_OFFLINE, this.HttpModbus, function()
{
IR.Log("Buffer: " + buffer);
IR.Log("Buffer!");
});
IR.AddListener(IR.EVENT_ONLINE, this.HttpModbus, function()
{
IR.Log("Online");
});

It does show data when the server responds with HTTP 200 OK:
04-17-2015 09:44:50 Kernel.Info 192.168.253.102 [17-04-2015 09:44:51.000] INFO Buffer!
04-17-2015 09:44:50 Kernel.Info 192.168.253.102 [17-04-2015 09:44:51.000] INFO Buffer: 192.168.253.102
04-17-2015 09:44:50 Kernel.Info 192.168.253.102 [17-04-2015 09:44:51.000] INFO Receive!
04-17-2015 09:44:50 Kernel.Info 192.168.253.102 [17-04-2015 09:44:51.000] INFO Receive text: 192.168.253.102
04-17-2015 09:44:50 Kernel.Info 192.168.253.102 [17-04-2015 09:44:51.000] INFO Server Event: Response!
04-17-2015 09:44:50 Kernel.Info 192.168.253.102 [17-04-2015 09:44:51.000] INFO Online

Still it doesn't show it was a 200 OK response.

When the server responds with a 404 or a 500 error code there is no response shown even though the event is triggered.
The log looks like this:
04-17-2015 09:42:39 Kernel.Info 192.168.253.102 [17-04-2015 09:42:39.000] INFO Buffer!
04-17-2015 09:42:39 Kernel.Info 192.168.253.102 [17-04-2015 09:42:39.000] INFO Receive!
04-17-2015 09:42:38 Kernel.Info 192.168.253.102 [17-04-2015 09:42:39.000] INFO Online

Complete IR.Log lines are missing.

Please let me know if you need any more information.
Answer
Answered
status can not be obtained from the server via HTTP driver
Answered
Status can not be obtained from the server via HTTP driver. But you can check for yourself. In order to to check the accessibility of the server (on EVENT_ONLINE). You need to check whether the buffer is empty. If it is not empty, then access to the server is. Otherwise, no access.




Then please consider this a feature request.
I would like to be able to read the response headers of an HTTP request.

There is a lot of useful information to be gathered from this:

HTTP/1.1 200 OK
Date: Fri, 17 Apr 2015 10:33:56 GMT
Server: thin 1.5.1 codename Straight Razor
Content-Type: text/html; charset=utf-8
X-UA-Compatible: IE=Edge,chrome=1
ETag: "ab31c645eacfc515be940caa09cfdb3c"
Cache-Control: must-revalidate, private, max-age=0
X-Request-Id: b7cf9ef740acb82ea130e62be7aae2e0
X-Runtime: 0.809662
X-Rack-Cache: miss
Vary: Accept-Encoding
Content-Encoding: gzip
Keep-Alive: timeout=15, max=100
Connection: Keep-Alive
Transfer-Encoding: chunked

I don't care if I have to parse this information myself, as long as it's available.
Debugging by checking if something does not exist is just not good enough. It could be a server error, typo, access denied, or something completely different.
Please let me know if this is possible in a future version.
Hi, R riksma!

We will discuss about the feature. The HTTP driver will be inproved in future version of the app, and may be this feature will be available. I will give feedback from discuss .
Answered
Hi, R riksma!

This feature will be added in future version of the app,
Hi Sergey,

Good to know.

thank you!