Back to Blog home

Introducing the Qt Http Server

Published on Friday January 25, 2019 by Jesús Fernández in Automation C++ Embedded HTML vs Qt Internet Internet of Things Labs Dev Loop web Websockets | Comments

First things first, Happy new year dear Qt users!

I'm sorry, I'm really late :(

Some time ago we decided to avoid repeating the same tedious code to write our own "Simple" Http Servers. So we started writing a Qt HTTP server with several essential features. The word simple is quoted because something planned to be "simple" evolved into a piece of code hard to maintain.
To give few numbers during the last two years, I wrote three times the same code to parse request headers, write response headers, deal with connections, ... And I know, I'm not the only one who did the same (just check GitHub).

To take a look into the code, you can clone it with:

git clone --recursive https://codereview.qt-project.org/qt-labs/qthttpserver

You can manually build it using the last Qt release.
Before listing the features let's show the mandatory hello world example:

#include <QtCore>
#include <QtHttpServer>

int main(int argc, char **argv) {
QCoreApplication app(argc, argv);
QHttpServer httpServer;
httpServer.route("/", []() {
return "Hello world";
});
httpServer.listen(QHostAddress::Any);
return app.exec();
}

At this moment it's a qt-labs project, and it's not officially part of the Qt distribution, but at some point, we'll add it.

Some features already implemented are:

  • HTTP 1.1
  • HTTPS but not yet merged
  • Customizable Routing
  • WebSockets integration
  • Error handling

I have planned some use-cases for this module:

  • Change the current embedded web server (and WebSockets) in the WebGL plugin to make it easy to create your own custom solutions based on the plugin.
  • Provide a way to serve an application built with WebAssamebly with live communication with the server via WebSockets and maybe using QtWebChannel.
  • Embed a web server into your application running on your desktop machine or embedded device.
  • Create a REST API
  • HTTP Server for QML
  • ...

If you're interested in this project, don't forget to clone it and maybe if you're concerned you can consider contributing or provide some feedback in QTBUG-60105.

Next week Mikhail Svetkin will write more about the routing API. Stay tuned :D

Subscribe to Our Blog

Stay up to date with the latest marketing, sales and service tips and news.

We are updating our comment system and you could face some issues. Please write to us at feedback@qt.io to report issues/bugs.