Giter VIP home page Giter VIP logo

crequests's Introduction

crequests

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:

  1. User defined SSL certificates.
  2. Iterative content dowload.
  3. File upload.

Thanks to: https://github.com/kennethreitz/requests https://github.com/whoshuu/cpr

crequests's People

Contributors

idfumg avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.