Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Author
Message
Max Tillberg
19
Years of Service
User Offline
Joined: 16th Jul 2005
Location:
Posted: 16th Jan 2018 22:50
I am trying to get data from a smart building system called OpenHAB and thought I use a REST call to get a Json file with timestamp and data. Any ideas how to do this in AppGameKit and read the data?
Sincerely
Max
Sincerely,
Max Tillberg
JohnnyMeek
11
Years of Service
User Offline
Joined: 23rd Apr 2013
Location: Slovenia
Posted: 17th Jan 2018 07:36
You have some basic HTTP commands that you can use, but they don't support headers. In some instances you also need to use PHP code hosted on a server.

You can check out this code I did for implementing the REST API of the Xtralife backend.

https://forum.thegamecreators.com/thread/219843
Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 17th Jan 2018 10:28 Edited at: 17th Jan 2018 10:37
there is a tool soap ui, you can make tests with it and see the raw request.
https://www.soapui.org/downloads/soapui.html (in case of an emergency)

in firefox menu developer / network you can see also the request data.
you can get also browser add-ons for RESTful web services.

Wiki REST
https://en.wikipedia.org/wiki/Representational_state_transfer

in agk i would try
SendHTTPRequest( iHTTP, szServerFile, szPostData )
GetHTTPFile( iHTTP, szServerFile, szLocalFile, szPostData )

Quote: " they don't support headers. "

away back i made a feature request for it

for pc we have also sockets commands but they not use secure.
AGK (Steam) V2017.12.12 : Windows 10 Pro 64 Bit : AMD (17.12.1) Radeon R7 265 : Mac mini OS High Sierra (10.13)
JohnnyMeek
11
Years of Service
User Offline
Joined: 23rd Apr 2013
Location: Slovenia
Posted: 17th Jan 2018 10:34
I've also requested it in the past.

They should really add proper support for HTTP, it's a basic requirement for a modern game engine. All other engines I've worked with supported it.
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 17th Jan 2018 17:08
What special headers would you even need to set for REST communication? Just request the specific resource through a basic http call then parse the response as you see fit.

Check out my description and example code at the bottom of this page. It'll give you an idea how to send requests and retrieve data from a site, in this case my hi-score website. (used to be called GameSpace)
http://purpletoken.com/faq.php
Tiled TMX Importer V.2
XML Parser V.2
Base64 Encoder/Decoder
Purple Token - Free online hi-score database
Legend of Zelda

"I like offending people, because I think people who get offended should be offended." - Linus Torvalds
TrezSoft
AGK Tool Maker
12
Years of Service
User Offline
Joined: 8th Nov 2012
Location: UK
Posted: 17th Jan 2018 17:45
Auth springs to mind :

Authorization: Bearer 0b79bab50daca910b000d4f1a2b675d604257e42

JohnnyMeek
11
Years of Service
User Offline
Joined: 23rd Apr 2013
Location: Slovenia
Posted: 17th Jan 2018 19:58 Edited at: 17th Jan 2018 20:01
How do you do a PUT or DELETE without propper http?

Plus as mentioned already, Auth?
Max Tillberg
19
Years of Service
User Offline
Joined: 16th Jul 2005
Location:
Posted: 18th Jan 2018 15:35
I have no idea what I am doing but I tried the following code and it did not work at alll. I would have hoped that temptext should be 24 since my rest call gave this result. Any ideas?
Sincerely,
Max Tillberg
Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 18th Jan 2018 16:02 Edited at: 18th Jan 2018 16:03
in this case szhost$ is without http:// and port :8080 and you need set a filename , use SetRawWritePath(GetReadPath()) and the output is in the project media folder
i can not test here because it is not a public url.

this using port 80 by default

see also SendHTTPRequestASync
AGK (Steam) V2017.12.12 : Windows 10 Pro 64 Bit : AMD (17.12.1) Radeon R7 265 : Mac mini OS High Sierra (10.13)
Max Tillberg
19
Years of Service
User Offline
Joined: 16th Jul 2005
Location:
Posted: 19th Jan 2018 16:37
Thanks a lot Markus. Unfortunately response$ is empty. Any idea how I can determine what is happening (or not happening)? There is a live demo for OpenHAB that can be called for example http://demo.openhab.org:8080/rest/items/Sun_Azimuth/state will giveyou the solar azimuth.
Sincerely,
Max Tillberg
Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 19th Jan 2018 17:08
Quote: " There is a live demo"

good, i think your problem is that agk using port 80 not 8080.
i will try using tcp socket for the web request.

here (screenshot below) how it looks if firefox make a request. (they are not showing the original but colourful ..)

AGK (Steam) V2017.12.12 : Windows 10 Pro 64 Bit : AMD (17.12.1) Radeon R7 265 : Mac mini OS High Sierra (10.13)

Attachments

Login to view attachments
Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 19th Jan 2018 18:57 Edited at: 19th Jan 2018 19:02
this works but only with a ip.
the answer data length is in the header as
Content-Length: 6

the header end with 2x cr lf, after this you get data

Dybing
13
Years of Service
User Offline
Joined: 12th Sep 2011
Location: Bergen, Norway
Posted: 20th Jan 2018 07:48 Edited at: 20th Jan 2018 07:57
On Windows, you have to use port 80 - on Linux, Android and iOS, any port will do. Probably on OS-X as well, but I have not tested that. Anyhow, here's my two standard functions for GET and POST methods in AGK:



Mind you, there are two globals here, the ip struct which gets its values from an external file at startup, and the httpOK variable that I use in other parts of the code to trigger different events based upon success or not of the API call. Also the false and true evals are defined as 0 and 1 respectively in the start of the main.agc file, like so:

#constant false = 0
#constant true = 1
#constant nil = -1

To use the two functions above, you just fill and send a query string to them. Like so with GET for instance:



...and for POST:

Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 20th Jan 2018 09:41
Quote: "On Windows, you have to use port 80"

if possible but you need to use the port that the server app is listening.
AGK (Steam) V2017.12.12 : Windows 10 Pro 64 Bit : AMD (17.12.1) Radeon R7 265 : Mac mini OS High Sierra (10.13)
Dybing
13
Years of Service
User Offline
Joined: 12th Sep 2011
Location: Bergen, Norway
Posted: 21st Jan 2018 12:44
> if possible but you need to use the port that the server app is listening.

Normally that would be true - but AppGameKit on Windows insist on using port 80 - though it will allow redirect to port 443. On other OS this is not an issue with AppGameKit, only on Windows.

A while back when I wrote the server app (written in Go, hosted on AWS) for my current project, I set the API IP to port 5000, because why not? Living entirely in linux-land, the AppGameKit client had no problems connecting to this port. Also allowed me to run server and client on the same machine (try doing that using port 80) for some fast iterations back and forth to flesh out the codebase on both server and client. When testing the client on Windows however, nothing. It flatly refused to use the assigned port. Be it on local or out to the AWS server.

So local testing at that point was out, and config files edited to set the port to 80 on both server and client and it worked. Though strangely, when I implemented TLS on the server, and set AppGameKit to use it as well, I did not have to manually set the client on Windows to port 443, it happily continued to work on the port 80 setting. I'm guessing the server sees the incoming request on port 80, goes this is no bueno, redirects internally to port 443, handles the request and replies on port 443 which the Windows client then pipes back to port 80.

On the other clients (Linux, Android and iOS) I've set the port to 443, but on Windows it have to be kept at 80. It is working though, so I am not complaining
Max Tillberg
19
Years of Service
User Offline
Joined: 16th Jul 2005
Location:
Posted: 25th Jan 2018 18:14
Thanks a lot for your help. So far I tried the example code from Marcus with no luck. I got no reply, not for port 8080 or 80. I tried it on a PC but the final version will run on an Andriod tablet. I also tried the following code by Dybing but that generated a 404 error. Any ideas what I am doing wrong? In the final application I would like to make about 10 rest calls to download data each minute but right now I would be extremly pleased if I could download one value at least.

Sincerely,
Max Tillberg
Max Tillberg
19
Years of Service
User Offline
Joined: 16th Jul 2005
Location:
Posted: 25th Jan 2018 18:33
My misstake, I removed ""getHash?catID="" and now it works very well.

Thanks for all help
Sincerely,
Max Tillberg
Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 25th Jan 2018 18:33 Edited at: 25th Jan 2018 18:34
this example should work at pc and android today, i make a wating loop for the connection state and also add a little more timeout time for the answer.

" and now it works very well."
ok,your example seems to using port 80, its much better than using socket.

AGK (Steam) V2017.12.12 : Windows 10 Pro 64 Bit : AMD (17.12.1) Radeon R7 265 : Mac mini OS High Sierra (10.13)
Max Tillberg
19
Years of Service
User Offline
Joined: 16th Jul 2005
Location:
Posted: 26th Jan 2018 08:16
I started to celebrate too soon. When I run the program on a local network where I can access OpenHAB, I must specify port 8080 like http://192.168.1.101:8080/rest/items/Foobot1PM/state . I have attached the code I use right now but how do I use port 8080 instead of the port that is used by default?
Sincerely,
Max Tillberg
Dybing
13
Years of Service
User Offline
Joined: 12th Sep 2011
Location: Bergen, Norway
Posted: 26th Jan 2018 09:41 Edited at: 26th Jan 2018 09:43
Change this:

SetHTTPHost(http, "192.168.1.101", 0)

to this:

SetHTTPHost(http, "192.168.1.101:8080", 0)

Though if on Windows, AppGameKit may complain since it is not port 80 - but iirc from the documentation port 8080 will do as well. Anything else (on windows) will not work. Linux, Android and iOS will happily accept any port being used however.
Max Tillberg
19
Years of Service
User Offline
Joined: 16th Jul 2005
Location:
Posted: 26th Jan 2018 10:11
Thanks a lot, I only tried that on my PC and it did not work.
Sincerely,
Max Tillberg
Dybing
13
Years of Service
User Offline
Joined: 12th Sep 2011
Location: Bergen, Norway
Posted: 26th Jan 2018 10:53 Edited at: 26th Jan 2018 13:35
Then you need do one of two things:

1: Ditch Windows as your dev environment. Linux is better for such tasks anyways, and distros like Ubuntu are pretty easy to set up - even for dual-boot.
2: Assuming your target is Android or iOS, you're going to have to stop trying to run the app on Windows for debugging/testing purposes, but rather broadcast to AppGameKit Player on your Android or iOS device.

That will work around your port problem. Though unless you can reconfigure your server to use port 80 (or better yet, add SSL/TLS to the mix and use port 443 on server, still port 80 on Windows client), the AppGameKit app on windows will not be able to reach it. See one of my posts above for my woes of AppGameKit on Windows port madness...

Login to post a reply

Server time is: 2024-11-24 04:07:02
Your offset time is: 2024-11-24 04:07:02