- c++11 http client based on libuv, http-parser and openssl
- a single header file
- http/https supports
#include <iostream>
#include "SimpleHttpRequest.hpp"
using namespace std;
using namespace request;
int main() {
SimpleHttpRequest request;
request.get("http://www.google.com")
.on("error", [](Error&& err){
cerr << err.name << endl << err.message << endl;
}).on("response", [](Response&& res){
cout << res.str();
}).end();
return 0;
}
string body = "{\"hello\": \"world\"}";
SimpleHttpRequest request;
request.setHeader("content-type","application/json")
.post("http://example.org:8080/", body)
.on("error", [](Error&& err){
cerr << err.name << endl << err.message << endl;
}).on("response", [](Response&& res){
cout << endl << res.statusCode << endl;
cout << res.str() << endl;
})
.end();
uv_loop = uv_default_loop();
map<string, string> options = {
{ "hostname", "google.com" },
{ "port" , "80" },
//{ "protocol", "https:" },
{ "path" , "/" },
{ "method" , "GET" }
};
SimpleHttpRequest request(options, uv_loop);
request.setHeader("content-type","application/json")
.on("error", [](Error&& err){
cerr << err.name << endl << err.message << endl;
}).on("response", [](Response&& res){
for (const auto &kv : res.headers)
cout << kv.first << " : " << kv.second << endl;
cout << res.str();
}).end();
return uv_run(uv_loop, UV_RUN_DEFAULT);
uv_loop = uv_default_loop();
map<string, string> options;
map<string, string> headers;
options["hostname"] = "example.org";
options["port"] = "80";
//options["protocol"] = "https:";
options["path"] = "/";
options["method"] = "POST";
headers["content-type"] = "application/json";
SimpleHttpRequest request(options, headers, uv_loop);
request.timeout = 1000;
request.on("error", [](Error&& err){
cerr << err.name << endl << err.message << endl;
});
request.on("response", [](Response&& res){
cout << endl << res.statusCode << endl;
cout << res.str() << endl;
});
request.write("{\"hello\":42}");
request.end();
return uv_run(uv_loop, UV_RUN_DEFAULT);
git clone --recursive https://github.com/ivere27/SimpleHttpRequest.git
cd SimpleHttpRequest
cd http-parser && make
cd ..
cd libuv && ./autogen.sh && ./configure && make
cd ..
# cd openssl && ./config && make
By uncommenting the lines in CMakeLists.txt
as indicated, instead of having
to build OpenSSL "in place," we can use Conan package manager.
See conan.io for more details; the TL;dr is pip install conan
.
# Create the .so library for http-parser:
cd http-parser && make library
ln -s libhttp_parser.so.2.7.1 libhttp_parser.so
# Build the package dependencies; currently just OpenSSL
mkdir .conan && cd .conan
conan install .. -s compiler=clang -s compiler.version=4.0 \
-s compiler.libcxx=libstdc++11 --build=missing
# Build the example binary.
mkdir build && cd build
cmake .. && cmake --build .
TODO: install step in CMake & detailed instruction how to use this in a C++ project
$ make
ENABLE_SSL macro (about 2.5MB will be added or $ strip a.out )
$ g++ example.cpp --std=c++11 \
-I./http-parser/ -I./libuv/include/ -I./openssl/include/ \
./libuv/.libs/libuv.a \
./http-parser/http_parser.o \
./openssl/libssl.a ./openssl/libcrypto.a \
-lpthread -ldl \
-DENABLE_SSL \
&& DEBUG=* ./a.out https://www.google.com
This is experimental yet. use at your own purpose!
- binary test
$ ./a.out http://www.google.com/images/nav_logo242.png > a.png
$ file a.png
a.png: PNG image data, 167 x 410, 8-bit/color RGBA, non-interlaced
- a project of 'Second Compiler'.
MIT