Best API for a REST-based client application
Loke (8740) 7 posts |
So I’ve been playing arounf with RiscOS for a bit, and it’s quite entertaining. Kind of what I want after working with highly complex systems for a long time. In order to learn, I decided to write a client for the Fediverse (the federated social network) using the Mastodon API’s. It’s a simple REST-based API with JSON input and output. It should be a simple enough API to work with when the main goal is to learn RiscOS programming. I’ve gotten as far as understanding how to manage the event loop, creating windows and so on. So the next step is to put something in those windows. That requires me to do some network programming which is why I’m posting this message. My needs are very simple: I need to be able to do basic REST calls using HTTPS and I need to parse and write JSON. For the HTTP part, I noted that OSLib has a system called URL, but when I experimented with those calls, the program compiled fine but when running the code it tells me that the SWI was now available. I then noticed that libcurl was available via PackMan but the README said it wasn’t well tested. I’m considering using that one instead, unless there is a better solution. For JSON I didn’t find anything. I searched these forums and found mentions of some library for it, but it’s not clear to me where I can find one. JSON is simple enough that I could write one, but surely there must be a recommended one. What are the recommendations for this specific use case? Note that I’m using C. |
Paolo Fabio Zaino (28) 1882 posts |
@ Loke
Right now the only done and available easy to use library for HTTP requests is HTTPLib from Chris Mahoney, otherwise you’ll have to use more complex ones. Chris’s library works on ROOL DDE compiler. If instead you want to go full on RESTFul and have a complete HTTP API you can use LibCurl3 (available on PackMan), but if you go this path then remember LibCurl requires UnixLib provided with GCC. For the JSON parsing you should be able to use JSON.c using GCC for RISC OS (or GCCSDK). Both LibCurl3 and JSON.c requires GCC for RISC OS, maybe someone already did the port of JSON.c to DDE (but if so I am not aware of it). If you do not want to use Libraries, then you could also use wget and cURL called as extern programs. And finally, If you can wait, then I am working on two modules one for fetching URL and posting content (GET and POST HTTP methods) and one to parse JSON (which is a port of JSON.c to DDE). ETA for both of them right now unfortunately is unknown given that I can only work a bit on each every weekend, due my busy job. If you need a tutorial of how to use libCurl3 with GCC on RISC OS, I can post some of my sources in my github.com and you can have a look at that. Hope this helps! |
Michael Gerbracht (180) 104 posts |
I was also looking at using REST with RISC OS and I wrote two little programs. I have also written an article about them for German GAG magazin but I plan to translate it and publish it in english but I am not sure when I will have time for it. However you can download both example programs and they include the C sources (using DDE, OSLib and HTTPLib). One (HTTPTest) is a program to test REST APIs, so you can send a request and get the result. It is very simple so don’t expect too much but it shows how to use the library. The other one is DLTrans which actually used the DeepL API for automatic translations and this one also includes cJSON to parse JSON. So this could also be a good starting point, again it is held very simple and you need an account if you want to try it. |
Chris Mahoney (1684) 2165 posts |
Which is here.
I’ve used cJSON. |
Loke (8740) 7 posts |
Thank you very much for your replies. I’m quite new to this, so I don’t know what DDE is. Based on your replies it’s a different C compiler. I’m using GCC, and I am also using Unixlib. Not necessarily because I have to but because seemed nice to have access to familiar API’s. Is using Unixlib a complication when distributing software? I’ve noted there are two versions of libcurl available. One is called libcurl and the other libcurl3. Based on the descriptions it’s not clear as to what the difference is. I’ve used libcurl on Linux in the past so as long as the RiscOS version hasn’t changed anything, it should be easy enough to get to the work. Finally, the API’s mentioned that use DDE, can I take advantage of them using GCC? Or are libraries tied to a specific compiler? |
Chris Mahoney (1684) 2165 posts |
DDE is short for Desktop Development Environment, which is available here. I’m not too familiar with GCC myself. The precompiled HTTPLib is intended for use with the DDE, but the source is available so hopefully it’s trivial to compile a GCC version. |
Paolo Fabio Zaino (28) 1882 posts |
LibCurl is (or should be) a port to RISC OS of libcurl 7.11.0-0 (11 Mar 2004) So, I would recommend to use LibCurl3. If you’ll decide to use libCurl3, then you won’t need the HTTPLib. If you’re used to use libcurl you know it deals with all the details (included HTTPS protocol). I used libcurl on Linux and, in terms of coding they are pretty much the same, no difference. While in terms of compilation then there are few difference due to RISC OS itself and some minor difference in GCC, this is something which I have noticed by few comments in the past that seems to be causing issues to quite a few people. So, few things to keep in mind:
In case of doubts, as I said, I can post my code on github.com, so you can have a look at all the details in the source and Makefiles |
Loke (8740) 7 posts |
Thank you very much for your help, and I am sorry for not replying earlier. I have succeeded in using both libcurl and cJSON, both of which works really well. I do have one followup question though. In order to be able to connect to any Mastodon servers using HTTPS, I’ve had to disable certificate validation. That suggests to me that the trusted CA’s are not up to date. What is it that I missed? |
Chris Mahoney (1684) 2165 posts |
The CA list lives in !Boot.Resources.!Internet.files.CertData. Check that the file is present and that it seems to be reasonably up to date (the version supplied with OS 5.28 is Wed Oct 14 03:12:15 2020 GMT). The date is listed inside the file (you can view it with any text editor). But… this is assuming that libcurl uses the system-provided CertData file. Does anyone know for sure? |
Chris Gransden (337) 1207 posts |
libcurl 7.74 is now available via PackMan. It uses the RISC OS CertData file for https. |
Paolo Fabio Zaino (28) 1882 posts |
Nice job Chris G.! :) |
David J. Ruck (33) 1635 posts |
I wouldn’t mind a simple rest server. All my Linux Pi’s run a tiny Python flask program to return the CPU temp and sensor readings to a logging process on my main Pi, and that would allow me to put the RISC OS machines in to the database too. |
Chris Mahoney (1684) 2165 posts |
Excellent! Thanks :) |
andrew k (267) 76 posts |
@Paolo If you still had your libcurl3 examples on GitHub I could make use of learning from them :) Libcurl3 doesn’t seem to work in the same way as the TBX library for compiling so I’m stuck on even compiling the most basic of HTTP examples from the CURL examples. *edit I have noticed that in packman if I check libcurl3 it doesn’t show any components that have been installed but for the libcurl3dev it shows components installed into apps/library/!LibCurl3. Before running g++ main.c++ I have double clicked on the !LibCurl3 like you do for GCC and TBX etc but I get errors as the compiler isn’t seeing the curl library. |
Chris Gransden (337) 1207 posts |
Here’s a simple makefile with the minmum needed for an example libcurl C program.
Also install the library dpendencies. LibCurl3-Dev LibSSL-Dev Zlib1g-Dev Lib-Cares-Dev. The Lib-Cares-Dev available via PackMan is an old version which causes an undefined symbol error when linking. |
andrew k (267) 76 posts |
Thanks Chris that has got me over my initial hurdle. @Loke Would be great to see Mastordon come to RISC OS! |