Skip to content
/ hypr Public

C++ HTTP client library based on libcurl

License

Notifications You must be signed in to change notification settings

erengy/hypr

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

78 Commits
 
 
 
 
 
 
 
 

Repository files navigation

hypr

hypr is an HTTP client library for C++. Provides an interface similar to Requests. Uses hypp and libcurl under the hood.

Usage

This is a work in progress. Usage in public applications is not yet recommended.

#include <iostream>
#include <hypr.hpp>

int main() {
  const auto r = hypr::get("https://example.com");

  std::cout << r.status_code() << '\n';           // 200
  std::cout << r.header("content-type") << '\n';  // text/html; charset=UTF-8
  std::cout << r.body().substr(0, 15) << '\n';    // <!doctype html>

  return 0;
}

Advanced Usage

const auto r = hypr::request("POST", "https://httpbin.org/post",
    // Optional parameters can be given in any order
    hypr::Query{{"a", "1"}, {"b", "2"}},
    hypr::Headers{
      {"Content-Type", "text/plain"},
      {"Referer", "https://github.com/erengy/hypr/"},
    },
    hypr::Body{"My body is ready."});

std::cout << r.url() << '\n';  // https://httpbin.org/post?a=1&b=2
// Requests can be built explicitly
hypr::Request request;
request.set_method("POST");
request.set_url("https://httpbin.org/post");
request.set_query({{"a", "1"}, {"b", "2"}});
request.set_headers({
    {"Content-Type", "text/plain"},
    {"Referer", "https://github.com/erengy/hypr/"}});
request.set_body("My body is ready.");

// Sessions can be reused
hypr::Session session;
const auto r = session.send(request);

Error Handling

// FTP is not supported, so we will get an error
const auto r = hypr::get("ftp://example.com");

if (r.error()) {
  std::cout << "Error: " << r.error().str() << '\n';  // Unsupported protocol
  return r.error().code;  // CURLE_UNSUPPORTED_PROTOCOL
}

License

Licensed under the MIT License.

About

C++ HTTP client library based on libcurl

Topics

Resources

License

Stars

Watchers

Forks

Languages