1 # A wrapper for the Qt Network Access API
3 This is just a wrapper around Qt's QNetworkAccessManager and friends. I use it in my apps at https://flavio.tordini.org . It has a simpler, higher-level API and some functionality not found in Qt:
5 - Throttling (as required by many web APIs nowadays)
7 - User agent and request header defaults
10 - Read timeouts (don't let your requests get stuck forever). (now supported by Qt >= 5.15)
11 - Redirection support (now supported by Qt >= 5.6)
15 This library uses the [Decorator design pattern](https://en.wikipedia.org/wiki/Decorator_pattern) to modularize features and make it easy to add them and use them as needed. The main class is [Http](https://github.com/flaviotordini/http/blob/master/src/http.h), which implements the base features of a HTTP client. More specialized classes are:
17 - [CachedHttp](https://github.com/flaviotordini/http/blob/master/src/cachedhttp.h), a simple disk-based cache
18 - [ThrottledHttp](https://github.com/flaviotordini/http/blob/master/src/throttledhttp.h), implements request throttling (aka limiting)
20 The constructor of these classes takes another Http instance for which they will act as a proxy. (See examples below). Following this design you can create your own Http subclass. For example, a different caching mechanism, an event dispatcher, custom request logging, etc.
24 In order to build this library you can use either `qmake` or `cmake`.
44 You can use this library as a git submodule. For example, add it to your project inside a lib subdirectory:
47 git submodule add -b master https://github.com/flaviotordini/http lib/http
50 Then you can update your git submodules like this:
53 git submodule update --init --recursive --remote
56 To integrate the library in your qmake based project just add this to your .pro file:
59 include(lib/http/http.pri)
62 qmake builds all object files in the same directory. In order to avoid filename clashes use:
65 CONFIG += object_parallel_to_source
68 If you are using CMake you can integrate the library by adding the following lines to your CMakeLists.txt:
71 add_subdirectory(lib/http)
73 target_link_library(your_super_cool_project <other libraries> http)
75 or if you have installed http you can find it via:
78 find_library(http REQUIRED)
80 target_link_library(your_super_cool_project <other libraries> http)
86 A basic C++14 example:
91 auto reply = Http::instance().get("https://google.com/");
92 connect(reply, &HttpReply::finished, this, [](auto &reply) {
93 if (reply.isSuccessful()) {
94 qDebug() << "Feel the bytes!" << reply.body();
96 qDebug() << "Something's wrong here" << reply.statusCode() << reply.reasonPhrase();
101 This is a real-world example of building a Http object with more complex features. It throttles requests, uses a custom user agent and caches results:
105 #include "cachedhttp.h"
106 #include "throttledhttp.h"
109 static Http *http = [] {
110 Http *http = new Http;
111 http->addRequestHeader("User-Agent", userAgent());
113 ThrottledHttp *throttledHttp = new ThrottledHttp(*http);
114 throttledHttp->setMilliseconds(1000);
116 CachedHttp *cachedHttp = new CachedHttp(*throttledHttp, "mycache");
117 cachedHttp->setMaxSeconds(86400 * 30);
125 If the full power (and complexity) of QNetworkReply is needed you can always fallback to it:
131 req.url = "https://flavio.tordini.org/";
132 QNetworkReply *reply = Http::instance().networkReply(req);
133 // Use QNetworkReply as needed...
136 Note that features like redirection, retries and read timeouts won't work in this mode.
140 You can use this library under the MIT license and at your own risk. If you do, you're welcome contributing your changes and fixes.