All Projects → BlackMATov → curly.hpp

BlackMATov / curly.hpp

Licence: MIT license
Simple cURL C++17 wrapper

Programming Languages

C++
36643 projects - #6 most used programming language

Projects that are alternatives of or similar to curly.hpp

request-extra
⚡️ Extremely stable HTTP request module built on top of libcurl with retries, timeouts and callback API
Stars: ✭ 14 (-70.83%)
Mutual labels:  curl, http-client, libcurl
Katipo
HTTP2 client for Erlang based on libcurl and libevent
Stars: ✭ 90 (+87.5%)
Mutual labels:  curl, http-client, libcurl
Curlsharp
CurlSharp - .Net binding and object-oriented wrapper for libcurl.
Stars: ✭ 153 (+218.75%)
Mutual labels:  curl, http-client, libcurl
Libhv
🔥 比libevent、libuv更易用的国产网络库。A c/c++ network library for developing TCP/UDP/SSL/HTTP/WebSocket client/server.
Stars: ✭ 3,355 (+6889.58%)
Mutual labels:  curl, http-client, requests
Php Curl Class
PHP Curl Class makes it easy to send HTTP requests and integrate with web APIs
Stars: ✭ 2,903 (+5947.92%)
Mutual labels:  curl, http-client, requests
Node Libcurl
libcurl bindings for Node.js
Stars: ✭ 447 (+831.25%)
Mutual labels:  curl, http-client, libcurl
Requester
Powerful, modern HTTP/REST client built on top of the Requests library
Stars: ✭ 273 (+468.75%)
Mutual labels:  curl, http-client, requests
Guzzle
Guzzle, an extensible PHP HTTP client
Stars: ✭ 21,384 (+44450%)
Mutual labels:  curl, http-client, requests
Ob Http
make http request within org-mode babel
Stars: ✭ 191 (+297.92%)
Mutual labels:  curl, http-client
Lua Curlv3
Lua binding to libcurl
Stars: ✭ 197 (+310.42%)
Mutual labels:  curl, libcurl
Httpie
As easy as /aitch-tee-tee-pie/ 🥧 Modern, user-friendly command-line HTTP client for the API era. JSON support, colors, sessions, downloads, plugins & more. https://twitter.com/httpie
Stars: ✭ 53,052 (+110425%)
Mutual labels:  curl, http-client
fennch
Modern fetch-based axios-like HTTP client for the browser and node.js
Stars: ✭ 12 (-75%)
Mutual labels:  http-client, requests
pawn-requests
pawn-requests provides an API for interacting with HTTP(S) JSON APIs.
Stars: ✭ 56 (+16.67%)
Mutual labels:  http-client, requests
Fetch
Asynchronous HTTP client with promises.
Stars: ✭ 29 (-39.58%)
Mutual labels:  curl, http-client
Httpp
Micro http server and client written in C++
Stars: ✭ 144 (+200%)
Mutual labels:  curl, http-client
Human curl
Simple Human wrapper for cURL library
Stars: ✭ 206 (+329.17%)
Mutual labels:  curl, requests
H2c
headers 2 curl. Provided a set of HTTP request headers, output the curl command line for generating that set. Try the converter online at
Stars: ✭ 113 (+135.42%)
Mutual labels:  curl, http-client
curlall
Simple curl-like CLI tool to automatically page through APIs
Stars: ✭ 25 (-47.92%)
Mutual labels:  curl, requests
fortran-curl
Fortran 2008 interface bindings to libcurl
Stars: ✭ 25 (-47.92%)
Mutual labels:  curl, libcurl
php-curl-cookbook
PHP CURL Cookbook 📖
Stars: ✭ 83 (+72.92%)
Mutual labels:  curl, http-client

curly.hpp

Simple cURL C++17 wrapper

linux darwin windows codecov language license

Features

  • Custom headers
  • Asynchronous requests
  • Different types of timeouts
  • URL encoded query parameters
  • Completion and progress callbacks
  • Custom uploading and downloading streams
  • PUT, GET, HEAD, POST, PATCH, DELETE, OPTIONS methods

Requirements

Installation

Just add the root repository directory to your cmake project:

add_subdirectory(external/curly.hpp)
target_link_libraries(your_project_target curly.hpp)

CMake options

  • USE_STATIC_CRT Use static C runtime library. Default: OFF
  • USE_SYSTEM_CURL Build with cURL from system paths. Default: OFF
  • USE_EMBEDDED_CURL Build with embedded cURL library. Default: ON

Examples

#include <curly.hpp/curly.hpp>
namespace net = curly_hpp;

// creates and hold a separate thread for automatically update async requests
net::performer performer;

// also, you can update requests manually from your favorite thread
net::perform();

GET Requests

// makes a GET request and async send it
auto request = net::request_builder()
    .method(net::http_method::GET)
    .url("http://www.httpbin.org/get")
    .send();

// synchronous waits and take a response
auto response = request.take();

// prints results
std::cout << "Status code: " << response.http_code() << std::endl;
std::cout << "Content type: " << response.headers["content-type"] << std::endl;
std::cout << "Body content: " << response.content.as_string_view() << std::endl;

// Status code: 200
// Content type: application/json
// Body content: {
//     "args": {},
//     "headers": {
//         "Accept": "*/*",
//         "Host": "www.httpbin.org",
//         "User-Agent": "cURL/7.54.0"
//     },
//     "origin": "37.195.66.134, 37.195.66.134",
//     "url": "https://www.httpbin.org/get"
// }

POST Requests

auto request = net::request_builder()
    .method(net::http_method::POST)
    .url("http://www.httpbin.org/post")
    .header("Content-Type", "application/json")
    .content(R"({"hello" : "world"})")
    .send();

auto response = request.take();
std::cout << "Body content: " << response.content.as_string_view() << std::endl;
std::cout << "Content Length: " << response.headers["content-length"] << std::endl;

// Body content: {
//     "args": {},
//     "data": "{\"hello\" : \"world\"}",
//     "files": {},
//     "form": {},
//     "headers": {
//         "Accept": "*/*",
//         "Content-Length": "19",
//         "Content-Type": "application/json",
//         "Host": "www.httpbin.org",
//         "User-Agent": "cURL/7.54.0"
//     },
//     "json": {
//         "hello": "world"
//     },
//     "origin": "37.195.66.134, 37.195.66.134",
//     "url": "https://www.httpbin.org/post"
// }
// Content Length: 389

Query Parameters

auto request = net::request_builder()
    .url("http://httpbin.org/anything")
    .qparam("hello", "world")
    .send();

auto response = request.take();
std::cout << "Last URL: " << response.last_url() << std::endl;

// Last URL: http://httpbin.org/anything?hello=world

Error Handling

auto request = net::request_builder()
    .url("http://unavailable.site.com")
    .send();

request.wait();

if ( request.is_done() ) {
    auto response = request.take();
    std::cout << "Status code: " << response.http_code() << std::endl;
} else {
    // throws net::exception because a response is unavailable
    // auto response = request.take();

    std::cout << "Error message: " << request.get_error() << std::endl;
}

// Error message: Could not resolve host: unavailable.site.com

Request Callbacks

auto request = net::request_builder("http://www.httpbin.org/get")
    .callback([](net::request request){
        if ( request.is_done() ) {
            auto response = request.take();
            std::cout << "Status code: " << response.http_code() << std::endl;
        } else {
            std::cout << "Error message: " << request.get_error() << std::endl;
        }
    }).send();

request.wait_callback();
// Status code: 200

Streamed Requests

Downloading

class file_dowloader : public net::download_handler {
public:
    file_dowloader(const char* filename)
    : stream_(filename, std::ofstream::binary) {}

    std::size_t write(const char* src, std::size_t size) override {
        stream_.write(src, static_cast<std::streamsize>(size));
        return size;
    }
private:
    std::ofstream stream_;
};

net::request_builder()
    .url("https://httpbin.org/image/jpeg")
    .downloader<file_dowloader>("image.jpeg")
    .send().wait();

Uploading

class file_uploader : public net::upload_handler {
public:
    file_uploader(const char* filename)
    : stream_(filename, std::ifstream::binary) {
        stream_.seekg(0, std::ios::end);
        size_ = static_cast<std::size_t>(stream_.tellg());
        stream_.seekg(0, std::ios::beg);
    }

    std::size_t size() const override {
        return size_;
    }

    std::size_t upload(char* dst, std::size_t size) override {
        stream_.read(dst, static_cast<std::streamsize>(size));
        return size;
    }
private:
    std::size_t size_{0u};
    std::ifstream stream_;
};

net::request_builder()
    .method(net::http_method::POST)
    .url("https://httpbin.org/anything")
    .uploader<file_uploader>("image.jpeg")
    .send().wait();

Promised Requests

Also, you can easily integrate promises like a promise.hpp.

#include <curly.hpp/curly.hpp>
namespace net = curly_hpp;

#include <promise.hpp/promise.hpp>
namespace netex = promise_hpp;

netex::promise<net::content_t> download(std::string url) {
    return netex::make_promise<net::content_t>([
        url = std::move(url)
    ](auto resolve, auto reject){
        net::request_builder(std::move(url))
            .callback([resolve,reject](net::request request) mutable {
                if ( !request.is_done() ) {
                    reject(net::exception("network error"));
                    return;
                }
                net::response response = request.take();
                if ( response.is_http_error() ) {
                    reject(net::exception("server error"));
                    return;
                }
                resolve(std::move(response.content));
            }).send();
    });
}

auto promise = download("https://httpbin.org/image/png")
    .then([](const net::content_t& content){
        std::cout << content.size() << " bytes downloaded" << std::endl;
    }).except([](std::exception_ptr e){
        try {
            std::rethrow_exception(e);
        } catch (const std::exception& ee) {
            std::cerr << "Failed to download: " << ee.what() << std::endl;
        }
    });

promise.wait();
// 8090 bytes downloaded
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].