SimpleHttpRequest
c++ http client for simple rest requests
Install / Use
/learn @ivere27/SimpleHttpRequestREADME
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
git clone with submodules.
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
Using CMake & Conan package
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
example.cpp - http
$ make
example.cpp - https
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
about
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'.
License
MIT
Related Skills
node-connect
344.4kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
99.2kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
344.4kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
344.4kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
