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:
This library uses the Decorator design pattern to modularize features and make it easy to add them and use them as needed. The main class is Http, which implements the base features of a HTTP client. More specialized classes are:
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.
In order to build this library you can use either qmake
or cmake
.
mkdir build
cd build
qmake ..
make
mkdir build
cd build
cmake ..
make
You can use this library as a git submodule. For example, add it to your project inside a lib subdirectory:
git submodule add -b master https://github.com/flaviotordini/http lib/http
Then you can update your git submodules like this:
git submodule update --init --recursive --remote
To integrate the library in your qmake based project just add this to your .pro file:
include(lib/http/http.pri)
qmake builds all object files in the same directory. In order to avoid filename clashes use:
CONFIG += object_parallel_to_source
If you are using CMake you can integrate the library by adding the following lines to your CMakeLists.txt:
add_subdirectory(lib/http)
...
target_link_library(your_super_cool_project <other libraries> http)
or if you have installed http you can find it via:
find_library(http REQUIRED)
...
target_link_library(your_super_cool_project <other libraries> http)
A basic C++14 example:
#include "http.h"
auto reply = Http::instance().get("https://flavio.tordini.org/");
connect(reply, &HttpReply::finished, this, [](auto &reply) {
if (reply.isSuccessful()) {
qDebug() << "Feel the bytes!" << reply.body();
} else {
qDebug() << "Something's wrong here" << reply.statusCode() << reply.reasonPhrase();
}
});
It is important to use the reply object passed by the finished
signal as it may be a different object than the one used to make the request. For example it may be created by CacheHttp
or other specialized Http
subclasses.
Example using two separate signals for success and failure:
#include "http.h"
auto reply = Http::instance().get("https://google.com/");
connect(reply, &HttpReply::data, this, [](auto &bytes) {
qDebug() << "Feel the bytes!" << bytes;
});
connect(reply, &HttpReply::error, this, [](auto &msg) {
qDebug() << "Something's wrong here" << msg;
});
A POST example taking advantage of C++11 initializer lists:
QUrl url("https://some.domain.url/");
auto reply = Http::instance().post(url, {{"myparam", "paramvalue"}, {"otherparam", "foo"}});
connect(reply, &HttpReply::finished, this, [](auto &reply) {
if (reply.isSuccessful()) {
qDebug() << "Feel the bytes!" << reply.body();
} else {
qDebug() << "Something's wrong here" << reply.statusCode() << reply.reasonPhrase();
}
});
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:
#include "http.h"
#include "cachedhttp.h"
#include "throttledhttp.h"
Http &myHttp() {
static Http *http = [] {
auto http = new Http;
http->addRequestHeader("User-Agent", "MyUserAgent");
auto throttledHttp = new ThrottledHttp(*http);
throttledHttp->setMilliseconds(1000);
auto cachedHttp = new CachedHttp(*throttledHttp, "mycache");
cachedHttp->setMaxSeconds(86400 * 30);
return cachedHttp;
}();
return *http;
}
If the full power (and complexity) of QNetworkReply is needed you can always fallback to it:
#include "http.h"
HttpRequest req;
req.url = "https://flavio.tordini.org/";
QNetworkReply *reply = Http::instance().networkReply(req);
// Use QNetworkReply as needed...
Note that features like redirection, retries and read timeouts won't work in this mode.
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.