- 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);
See conan.io for more details; the TL;dr is pip install conan
.
The easiest way is to just use the build.sh
script; see that file for details as to how to run the build process manually.
If $INSTALL_DIR
is defined, the additional install
target will be generated by CMake, and running make install
will cause the shared libraries and header files to be copied to the lib
and include
subdirectories of $INSTALL_DIR
, respectively.
Alternatively, these files can be found in the same directories of the build
directory.
For reasons that are not entirely clear, when building http-parser
, it requires the node-gyp
library, and this will fail with a cryptic error:
xcode-select: error: tool 'xcodebuild' requires Xcode, but active developer directory '/Library/Developer/CommandLineTools' is a command line tools instance
The solution is to execute:
sudo xcode-select -s /Applications/Xcode.app/Contents/Developer
and once the build is complete, reset the value to:
sudo xcode-select -s /Library/Developer/CommandLineTools
otherwise, building sources will fail with many (many) errors such as:
/Library/Developer/CommandLineTools/usr/include/c++/v1/wchar.h:137:77: error: use of undeclared identifier
'wcschr'
wchar_t* __libcpp_wcschr(const wchar_t* __s, wchar_t __c) {return (wchar_t*)wcschr(__s, __c);}
Use the ENABLE_SSL macro (about 2.5MB will be added or $ strip a.out
) during the compilation step; this could be done like this:
$ g++ example.cpp --std=c++11 \
-I${INSTALL_DIR}/include/ \
-L ${INSTALL_DIR}/lib/ \
-lpthread -ldl -lssl -luv -lcrypto -lhttp_parser \
-DENABLE_SSL \
&& DEBUG=* ./a.out https://www.google.com
or via CMake:
target_compile_definitions(example_with_ssl PUBLIC ENABLE_SSL)
MIT