19.3.2015
Few weeks ago I described simple http server for Qt. How we use the server?
We use the server for embedded application built on BeagleBone Black. Of course, we do not want to generate HTML code in C++ so we try to make it simple and all pages have unified structure.
Do you need modern application to collect you data or to control your processes?
Call: +420 777 566 384 or email to address info@hobrasoft.cz
API is not direct part of server. It can be found in an example bundled with server. Brief description can be found in class documentation AbstractController.
Most data structures and access to applications data are made in the same manner. Application shows simple list of items, clicking the item form is opened, clicking to "Save" button the changes are stored, clicking to "Delete" the item is deleted.
Requests served by AbstractController class look uniformly:
GET http://localhost:8086/room // Returns rooms list GET http://localhost:8086/room/events // Event stream, all rooms GET http://localhost:8086/room/e40f2a // Returns room id=e40f2a GET http://localhost:8086/room/e40f2a/events // Event stream, only single room
To access data also other HTTP methods can be used: GET, PUT, POST and DELETE. Methods PUT a POST are used to store items, method DELETE is used to delete the item:
PUT http://localhost:8086/room/e40f2a // Stores passed data DELETE http://localhost:8086/room/e40f2a // Deletes room id=e40f2a
Following virtual methods can be overloaded in the class AbstractController:
Is is sufficient to derive only such method which you want to use. Implementation can be very simple:
void ControllerRoom::serviceIdGet( HobrasoftHttp::HttpRequest *request, HobrasoftHttp::HttpResponse *response, const QString& id ) { // Check is not needed // ROOMSLIST is an object with list of all rooms // Method room(id) return the room class instance serviceOK(request, response, ROOMSLIST->room(id)) }
Method serviceOK() requests the abstract class to return HTTP reply "200 OK" with passed data. Data structure is described bellow. All data are converted to JSON format.
If you want to send an error message instead of "200 OK", use the serviceError() method.
But is is not needed to make basic check. Abstract class cares about basic checks - you only have to reimplement the exists() method - it should return true, if the ID exists:
bool ControllerRoom::exists(const QString& id) { return ROOMSLIST->contains(id); }
It is not needed to reimplement methods which are not used actually. Default implementation return error 501.
Data are passed in structured container QVariantMap. You can create the structure like this:
QVariantMap data; data["ID"] = "e40f2a"; data["Description"] = "Name of the room"; QVariantList list; list << "abc" << "xyz"; data["List"] = list;
Corresponding JSON structure look like this:
{ "ID" : "e40f2a", "Description" : "Name of the room", "List" : [ "abc", "xyz" ] }
Are you interested in HTTP C++ server? Stay tuned, watch our twitter or this blog. We have prepared article about HTML5 event stream for you.