Skip to content

daminetreg/cpr

 
 

Repository files navigation

C++ Requests: Curl for People

Build Status Build status Coverage Status Documentation

C++ Requests is a simple wrapper around libcurl inspired by the excellent Python Requests project.

Despite its name, libcurl's easy interface is anything but, and making mistakes misusing it is a common source of error and frustration. Using the more expressive language facilities of C++11, this library captures the essence of making network calls into a few concise idioms.

Here's a quick GET request:

#include <cpr/cpr.h>

int main(int argc, char** argv) {
    auto r = cpr::Get(cpr::Url{"https://api.github.com/repos/whoshuu/cpr/contributors"},
                      cpr::Authentication{"user", "pass"},
                      cpr::Parameters{{"anon", "true"}, {"key", "value"}});
    r.status_code;                  // 200
    r.header["content-type"];       // application/json; charset=utf-8
    r.text;                         // JSON text string
}

And here's less functional, more complicated code, without cpr.

Documentation

You can find the latest documentation here. It's a work in progress, but it should give you a better idea of how to use the library than the tests currently do.

Features

C++ Requests currently supports:

  • Custom headers
  • Url encoded parameters
  • Url encoded POST values
  • Multipart form POST upload
  • File POST upload
  • Basic authentication
  • Digest authentication
  • Timeout specification
  • Asynchronous requests
  • 🍪 support!
  • Proxy support
  • Callback interface
  • PUT methods
  • DELETE methods
  • HEAD methods
  • OPTIONS methods
  • PATCH methods

Planned

Support for the following will be forthcoming (in rough order of implementation priority):

and much more!

Usage

For just getting this library up and running, I highly recommend forking the example project. It's configured with the minimum CMake magic and boilerplate needed to start playing around with networked applications.

If you already have a project you need to integrate C++ Requests with, the primary way is to use git submodules. Add this repository as a submodule of your root repository:

git submodule add git@github.com:whoshuu/cpr.git
git submodule update --init --recursive

Next, add this subdirectory to your CMakeLists.txt before declaring any targets that might use it:

add_subdirectory(cpr)

This will produce two important CMake variables, CPR_INCLUDE_DIRS and CPR_LIBRARIES, which you'll use in the typical way:

include_directories(${CPR_INCLUDE_DIRS})
target_link_libraries(your_target_name ${CPR_LIBRARIES})

and that should do it! Using the submodule method of integrating C++ Requests, there's no need to handle libcurl yourself, all of those dependencies are taken care of for you.

Depending on the library from multiple apps with find_package

Simply install the library on your system :

mkdir build/
cmake .. -DUSE_SYSTEM_CURL=ON -DBUILD_CPR_TESTS=OFF
make install

And then simply create your CMake project as such, include directories and libraries to link will be forwarded to your target. :

cmake_minimum_required(VERSION 3.0.0)

project(yourproject)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

find_package(cpr 1.2.1 REQUIRED)

add_executable(yourproject main.cpp)
target_link_libraries(yourproject cpr::cpr)

Requirements

The only explicit requirements are:

  • a C++11 compatible compiler such as Clang or GCC. The minimum required version of GCC is unknown, so if anyone has trouble building this library with a specific version of GCC, do let me know
  • curl and its development libraries

Contributing

Please fork this repository and contribute back using pull requests. Features can be requested using issues. All code, comments, and critiques are greatly appreciated.

About

Fork to add CMake Package Config support to CPR

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C++ 95.4%
  • CMake 4.2%
  • Other 0.4%