Skip to content

massenz/SimpleHttpRequest

 
 

Repository files navigation

SimpleHttpRequest

  • c++11 http client based on libuv, http-parser and openssl
  • a single header file
  • http/https supports

Usage

.get , .post , .put , .del

#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();

with options, setHeader

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);

with options, headers, timeout

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);

build & test

Using CMake & Conan package

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.

Building node-gyp

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);}

https support

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)

License

MIT

About

c++ http client for simple rest requests

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C++ 88.3%
  • CMake 8.1%
  • Shell 1.9%
  • Makefile 1.7%