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?
S BleMac="DC:0D:30:9E:3A:EC",GatewayMac="CC:1B:E0:E2:56:18"
S 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
S ^check("1222")=HttpURL_"|"_Token
Set tSc=Httprequest.Get(HttpURL)
S ^check("122")=1
s ^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
.png)
Thanks
Comments
I can’t find the information you’re looking for. If you rephrase your question, I might be able to help. You can also explore the following resources.
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):5if$test=0 {
write"- Failed to connect"close device
quit
}
use device
write"GET "_endpoint_" HTTP/1.0"_$c(13,10),*-3set (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 messageread buffer:5if$test=0 {
use0write"- Failed to read anything",!
zw buffer
close device
quit
}
// Write ":ok" so API begins sending datawrite":ok"_$c(13,10)
read buffer:5use0if$test=0 {
use0write"- Failed to read anything",!
zw buffer
close device
quit
}
use0zw buffer
close device
quit;Best regards
Ludwig
My URL is like :
so I guess from your code url="ac1.mqtt.sx3ac.com" and endpoint ="/api/gap/nodes?filter_mac=DC:0D:30:C6:19:9D&filter_rssi=-75&mac=CC:1B:E0:E4:49:28&active=1&event=1&chip=1&access_token=437099c4a30895840ca7439d38e974d7c38338e72a5190abc7e47b14bcb4094d" ?
and in open device: HOSTNAME is the server where my program is running (IRIS install) or the server I'm connecting to? and SSL would be my SSL/TLS config?
Thanks