c++ HTTP library inspired by python requests.
Library dependecies: OpenSSL Boost C++11
This library is created for making comfortable way to do HTTP requests. You can do simple ordinary requests like this:
int main() {
using namespace crequests;
service_t service;
auto response = Get(service, "http://yandex.ru");
std::cout << reponse->content() << std::endl;
return 0;
}
Which is printed content of downloaded web page.
Also, you have a full control on result of request operation:
using namespace crequests;
service_t service;
auto response = Get(service, "http://yandex.ru");
if (response.error()) {
if (response.error().code() == error_code_t::TIMEOUT) {
// handle timeout
}
else {
// handle other error
}
}
If you want to save intermediate data between requests like cookies, you can use session mechanism:
using namespace crequests;
service_t service;
auto session = service.new_sesion("http://yandex.ru", timeout_t{10});
{
auto response = session->Get();
std::cout << response->content() << std::endl;
}
{
// send new request with saved cookies and settings.
set_option(session, timeout_t{2}); // you can change parameters arbitrarily.
auto response = session->Get();
std::cout << response->content() << std::endl;
}
Also, its available to use asynchronous nature of Boost.Asio to send multiple requests. Ordinary HTTP os HTTPS protocol will be choosen from a given uri. Also you can implicitly set it by sending parameters to api functions.
using namespace crequests;
static const std::vector urls = {
"http://google.ru",
"http://httpbin.org",
"https://www.google.ru",
"http://youtube.com",
"http://yandex.ru",
"http://ya.ru",
"http://boost.org",
"http://www.vk.com"
};
std::vector< future_t< response_ptr_t> > futures;
service_t service;
for (auto&& url : urls)
futures.push_back(AsyncGet(service, url));
for (auto&& future : futures)
print(future.get()->error());
If you do not want to explicit wait response from server you can set final callback to do the work. This feature is needed if client is running in separate thread or process and you want to grab results later. All received responses will be saved for a dispose_timeout interval. After timeout they will be removed. So you can get a response when you want and adjust settings as you want.
using namespace crequests;
service_t service{dispose_timeout_t{10}};
auto callback = [](const response_t& response) {
// do the work
};
AsyncGet(service, "http://boost.org", final_callback_t{callback});
service.run(); // if this is end of program, otherwise it will work in separate thread infinitely.
KeepAlive and redirects is on by default. You can gzip you POST data on demand by using gzip_t{true} on api functions.
response->raw() function return raw data received from the server. response->content() function return ungzipped data (if needed or raw data) automatically.
TODO:
- User defined SSL certificates.
- Iterative content dowload.
- File upload.
Thanks to: https://github.com/kennethreitz/requests https://github.com/whoshuu/cpr