Question
· Jun 23

HTTP Request not returning data

Hi Guys,

I'm using below to retrieve advertising data from Cassia AC server, but the problem is that its a live connection so the Httprequest.Get(HttpURL) call will hang and doesn't exit & return the data so is there a way for return and after say 30 secs from this live data? or is there something EventSource or something similar where I can pass the URL call then after say 30 secs I can end & exit the call to return the collected data?

  

 BleMac="DC:0D:30:9E:3A:EC",GatewayMac="CC:1B:E0:E2:56:18"
Token=##class(SX3.Task.TemperatureCollection).GetAuth()
Set Httprequest=##class(%Net.HttpRequest).%New()
Set Httprequest.SSLConfiguration="RTLS"
Set Httprequest.Server="MyURL"
Set Httprequest.Timeout=30
Set Httprequest.Https=1
//S Httprequest.WriteTimeout=10
set Httprequest.ContentType="application/json"
Do Httprequest.SetHeader("Accept","application/json")
Do Httprequest.SetHeader("Accept-Language","en_US")
Do Httprequest.SetHeader("Authorization","Bearer "_Token)
Set HttpURL="/api/gap/nodes?filter_mac="_BleMac_"&filter_rssi=-75&mac="_GatewayMac_"&active=1&event=1&chip=1&access_token="_Token
^check("1222")=HttpURL_"|"_Token
Set tSc=Httprequest.Get(HttpURL)
^check("122")=1
^data=Httprequest.HttpResponse.Data.Read(Httprequest.HttpResponse.Data.Size)
//W !,Httprequest.HttpResponse.Data.ReadLine()
//S obj=##class(%ZEN.Auxiliary.jsonProvider).%ConvertJSONToObject(Httprequest.HttpResponse.Data.Read(Httprequest.HttpResponse.Data.Size),,.list)

here how it looks if I run the URL in the browser

 

 

Thanks

Product version: IRIS 2024.3
Discussion (3)2
Log in or sign up to continue

So basically I have a URL call that connect to server and continuously populate data so if I run that URL in a browser you will that data coming thought the browser and stopping till you close the browser, and they problem is if I use the same call using HTTP request I can't get any response because I guess didn't finish and will never finish collecting data to a response, so the solution is either to find a way to end the collection of HTTP request so can get a response, or use EventSource like in Javascript, below is a javascript code that can do it

var es = new EventSource('Myurl')
    checkTime();
    var startTime = new Date().getTime()
    es.onmessage = function(e) {
    var endTime=new Date().getTime();
    console.log(e.data);
    if ((endTime-startTime)>20000){es.close();return;}};
    es.onerror = function(e) {console.log('ERROR!'+e.status);es.close();return; };
    function checkTime() {
    setTimeout(function() {
    es.close();
    return;
    }, 60000);}

Thanks          

Hi Nezla,

Unfortunately, I don't know if it's possible to achieve what you want by using %Net.HttpRequest. 
If the data is that simple, I'd probably try and handle the HTTP communication manually over TCP.

Please be aware that this is just a quick code snippet, and you'll probably need to refine it further to suit your needs. In the open command you can alter /KEEPALIVE=n to keep the connection alive for n seconds where n>30 and n<432000

randomFunction
 new device,buffer,key,val,iterator
 new url,endpoint,http,headers
 
 set url="stream.wikimedia.org"
 set endpoint="/v2/stream/recentchange"
 set headers = {}
 do headers.%Set("Accept","*/*")
 do headers.%Set("Host",url)
 do headers.%Set("User-Agent","TestClient")
 
 set device="|TCP|2"
 open device:(/HOSTNAME="185.15.59.224":/PORT=443:/SSL="test":/STREAM=1:/KEEPALIVE=30):5
 if $test=0 {
    write "- Failed to connect"
    close device
    quit
 }
 
 use device
 
 write "GET "_endpoint_" HTTP/1.0"_$c(13,10),*-3
 set (key,value,type)=""
 set iterator=headers.%GetIterator()
 while iterator.%GetNext(.key,.value,) {
    write key_":"_value_$c(13,10),*-3
 }
 write $c(13,10,13,10),*-3
 // Ignore first message
 read buffer:5
 if $test=0 {
    use 0
    write "- Failed to read anything",!
    zw buffer
    close device
    quit
 }
 
 // Write ":ok" so API begins sending data
 write ":ok"_$c(13,10)
 
 read buffer:5
 use 0
 if $test=0 {
    use 0
    write "- Failed to read anything",!
    zw buffer
    close device
    quit
 }
 
 use 0
 zw buffer
 close device
 quit
 ;

Best regards
Ludwig