|
| 1 | +--- |
| 2 | +layout: project |
| 3 | +title: restclient-cpp |
| 4 | +--- |
| 5 | +# REST client for C++ |
| 6 | +[](https://travis-ci.org/mrtazz/restclient-cpp) |
| 7 | +[](https://coveralls.io/github/mrtazz/restclient-cpp?branch=master) |
| 8 | +[](https://packagecloud.io/mrtazz/restclient-cpp) |
| 9 | +[](http://code.mrtazz.com/restclient-cpp/ref/) |
| 10 | +[](http://opensource.org/licenses/MIT) |
| 11 | + |
| 12 | + |
| 13 | +## About |
| 14 | +This is a simple REST client for C++. It wraps [libcurl][] for HTTP requests. |
| 15 | + |
| 16 | +## Usage |
| 17 | +restclient-cpp provides two ways of interacting with REST endpoints. There is |
| 18 | +a simple one, which doesn't need you to configure an object to interact with |
| 19 | +an API. However the simple way doesn't provide a lot of configuration options |
| 20 | +either. So if you need more than just a simple HTTP call, you will probably |
| 21 | +want to check out the advanced usage. |
| 22 | + |
| 23 | +### Simple Usage |
| 24 | +The simple API is just some static methods modeled after the most common HTTP |
| 25 | +verbs: |
| 26 | + |
| 27 | +```cpp |
| 28 | +#include "restclient-cpp/restclient.h" |
| 29 | + |
| 30 | +RestClient::Response r = RestClient::get("http://url.com") |
| 31 | +RestClient::Response r = RestClient::post("http://url.com/post", "text/json", "{\"foo\": \"bla\"}") |
| 32 | +RestClient::Response r = RestClient::put("http://url.com/put", "text/json", "{\"foo\": \"bla\"}") |
| 33 | +RestClient::Response r = RestClient::del("http://url.com/delete") |
| 34 | +RestClient::Response r = RestClient::head("http://url.com") |
| 35 | +``` |
| 36 | + |
| 37 | +The response is of type [RestClient::Response][restclient_response] and has |
| 38 | +three attributes: |
| 39 | + |
| 40 | +```cpp |
| 41 | +RestClient::Response.code // HTTP response code |
| 42 | +RestClient::Response.body // HTTP response body |
| 43 | +RestClient::Response.headers // HTTP response headers |
| 44 | +``` |
| 45 | + |
| 46 | +### Advanced Usage |
| 47 | +However if you want more sophisticated features like connection reuse, |
| 48 | +timeouts or authentication, there is also a different, more configurable way. |
| 49 | + |
| 50 | +```cpp |
| 51 | +#include "restclient-cpp/connection.h" |
| 52 | +#include "restclient-cpp/restclient.h" |
| 53 | + |
| 54 | +// initialize RestClient |
| 55 | +RestClient::init(); |
| 56 | + |
| 57 | +// get a connection object |
| 58 | +RestClient::Connection* conn = new RestClient::Connection("http://url.com"); |
| 59 | + |
| 60 | +// configure basic auth |
| 61 | +conn->SetBasicAuth("WarMachine68", "WARMACHINEROX"); |
| 62 | + |
| 63 | +// set connection timeout to 5s |
| 64 | +conn->SetTimeout(5); |
| 65 | + |
| 66 | +// set custom user agent |
| 67 | +// (this will result in the UA "foo/cool restclient-cpp/VERSION") |
| 68 | +conn->SetUserAgent("foo/cool"); |
| 69 | + |
| 70 | +// enable following of redirects (default is off) |
| 71 | +conn->FollowRedirects(true); |
| 72 | + |
| 73 | +// set headers |
| 74 | +RestClient::HeaderFields headers; |
| 75 | +headers["Accept"] = "application/json"; |
| 76 | +conn->SetHeaders(headers) |
| 77 | + |
| 78 | +// append additional headers |
| 79 | +conn->AppendHeader("X-MY-HEADER", "foo") |
| 80 | + |
| 81 | +// if using a non-standard Certificate Authority (CA) trust file |
| 82 | +conn->SetCAInfoFilePath("/etc/custom-ca.crt") |
| 83 | + |
| 84 | +RestClient::Response r = conn->get("/get") |
| 85 | +RestClient::Response r = conn->head("/get") |
| 86 | +RestClient::Response r = conn->del("/delete") |
| 87 | + |
| 88 | +// set different content header for POST and PUT |
| 89 | +conn->AppendHeader("Content-Type", "text/json") |
| 90 | +RestClient::Response r = conn->post("/post", "{\"foo\": \"bla\"}") |
| 91 | +RestClient::Response r = conn->put("/put", "text/json", "{\"foo\": \"bla\"}") |
| 92 | + |
| 93 | +// deinit RestClient. After calling this you have to call RestClient::init() |
| 94 | +// again before you can use it |
| 95 | +RestClient::disable(); |
| 96 | +``` |
| 97 | +
|
| 98 | +The responses are again of type [RestClient::Response][restclient_response] |
| 99 | +and have three attributes: |
| 100 | +
|
| 101 | +```cpp |
| 102 | +RestClient::Response.code // HTTP response code |
| 103 | +RestClient::Response.body // HTTP response body |
| 104 | +RestClient::Response.headers // HTTP response headers |
| 105 | +``` |
| 106 | + |
| 107 | +The connection object also provides a simple way to get some diagnostics and |
| 108 | +metrics information via `conn->GetInfo()`. The result is a |
| 109 | +`RestClient::Connection::Info` struct and looks like this: |
| 110 | + |
| 111 | +```cpp |
| 112 | +typedef struct { |
| 113 | + std::string base_url; |
| 114 | + RestClients::HeaderFields headers; |
| 115 | + int timeout; |
| 116 | + struct { |
| 117 | + std::string username; |
| 118 | + std::string password; |
| 119 | + } basicAuth; |
| 120 | + std::string customUserAgent; |
| 121 | + struct { |
| 122 | + // total time of the last request in seconds Total time of previous |
| 123 | + // transfer. See CURLINFO_TOTAL_TIME |
| 124 | + int totalTime; |
| 125 | + // time spent in DNS lookup in seconds Time from start until name |
| 126 | + // resolving completed. See CURLINFO_NAMELOOKUP_TIME |
| 127 | + int nameLookupTime; |
| 128 | + // time it took until Time from start until remote host or proxy |
| 129 | + // completed. See CURLINFO_CONNECT_TIME |
| 130 | + int connectTime; |
| 131 | + // Time from start until SSL/SSH handshake completed. See |
| 132 | + // CURLINFO_APPCONNECT_TIME |
| 133 | + int appConnectTime; |
| 134 | + // Time from start until just before the transfer begins. See |
| 135 | + // CURLINFO_PRETRANSFER_TIME |
| 136 | + int preTransferTime; |
| 137 | + // Time from start until just when the first byte is received. See |
| 138 | + // CURLINFO_STARTTRANSFER_TIME |
| 139 | + int startTransferTime; |
| 140 | + // Time taken for all redirect steps before the final transfer. See |
| 141 | + // CURLINFO_REDIRECT_TIME |
| 142 | + int redirectTime; |
| 143 | + // number of redirects followed. See CURLINFO_REDIRECT_COUNT |
| 144 | + int redirectCount; |
| 145 | + } lastRequest; |
| 146 | +} Info; |
| 147 | +``` |
| 148 | + |
| 149 | +#### Persistent connections/Keep-Alive |
| 150 | +The connection object stores the curl easy handle in an instance variable and |
| 151 | +uses that for the lifetime of the object. This means curl will [automatically |
| 152 | +reuse connections][curl_keepalive] made with that handle. |
| 153 | + |
| 154 | + |
| 155 | +## Thread Safety |
| 156 | +restclient-cpp leans heavily on libcurl as it aims to provide a thin wrapper |
| 157 | +around it. This means it adheres to the basic level of thread safety [provided |
| 158 | +by libcurl][curl_threadsafety]. The `RestClient::init()` and |
| 159 | +`RestClient::disable()` methods basically correspond to `curl_global_init` and |
| 160 | +`curl_global_cleanup` and thus need to be called right at the beginning of |
| 161 | +your program and before shutdown respectively. These set up the environment |
| 162 | +and are **not thread-safe**. After that you can create connection objects in |
| 163 | +your threads. Do not share connection objects across threads as this would |
| 164 | +mean accessing curl handles from multiple threads at the same time which is |
| 165 | +not allowed. |
| 166 | + |
| 167 | +In order to provide an easy to use API, the simple usage via the static |
| 168 | +methods implicitly calls the curl global functions and is therefore also **not |
| 169 | +thread-safe**. |
| 170 | + |
| 171 | + |
| 172 | +## Dependencies |
| 173 | +- [libcurl][] |
| 174 | + |
| 175 | +## Installation |
| 176 | +There are some packages available for Linux on [packagecloud][packagecloud]. |
| 177 | +And for OSX you can get it from the mrtazz/oss homebrew tap: |
| 178 | + |
| 179 | +```bash |
| 180 | +brew tap mrtazz/oss |
| 181 | +brew install restclient-cpp |
| 182 | +``` |
| 183 | + |
| 184 | +Otherwise you can do the regular autotools dance: |
| 185 | + |
| 186 | +```bash |
| 187 | +./autogen.sh |
| 188 | +./configure |
| 189 | +make install |
| 190 | +``` |
| 191 | + |
| 192 | +## Contribute |
| 193 | +All contributions are highly appreciated. This includes filing issues, |
| 194 | +updating documentation and writing code. Please take a look at the |
| 195 | +[contributing guidelines][contributing] before so your contribution can be |
| 196 | +merged as fast as possible. |
| 197 | + |
| 198 | + |
| 199 | +[libcurl]: http://curl.haxx.se/libcurl/ |
| 200 | +[gtest]: http://code.google.com/p/googletest/ |
| 201 | +[packagecloud]: https://packagecloud.io/mrtazz/restclient-cpp |
| 202 | +[contributing]: https://github.com/mrtazz/restclient-cpp/blob/master/CONTRIBUTING.md |
| 203 | +[curl_keepalive]: http://curl.haxx.se/docs/faq.html#What_about_Keep_Alive_or_persist |
| 204 | +[curl_threadsafety]: http://curl.haxx.se/libcurl/c/threadsafe.html |
| 205 | +[restclient_response]: http://code.mrtazz.com/restclient-cpp/ref/struct_rest_client_1_1_response.html |
0 commit comments