https://github.com/erengy/hypr
C++ HTTP client library based on libcurl
https://github.com/erengy/hypr
cpp17 header-only http work-in-progress
Last synced: 8 months ago
JSON representation
C++ HTTP client library based on libcurl
- Host: GitHub
- URL: https://github.com/erengy/hypr
- Owner: erengy
- License: mit
- Created: 2019-08-29T20:14:39.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2022-10-22T12:09:13.000Z (over 3 years ago)
- Last Synced: 2025-02-15T17:43:07.957Z (12 months ago)
- Topics: cpp17, header-only, http, work-in-progress
- Language: C++
- Homepage:
- Size: 58.6 KB
- Stars: 2
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# hypr
hypr is an HTTP client library for C++. Provides an interface similar to [Requests](https://requests.readthedocs.io/). Uses [hypp](https://github.com/erengy/hypp/) and [libcurl](https://curl.haxx.se/) under the hood.
## Usage
***This is a work in progress. Usage in public applications is not yet recommended.***
```cpp
#include
#include
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'; //
return 0;
}
```
### Advanced Usage
```cpp
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
```
```cpp
// 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
```cpp
// 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](https://opensource.org/licenses/MIT).