|
1 | | -# requests |
2 | | -HTTP for C++ |
| 1 | +# vix/requests |
| 2 | + |
| 3 | +HTTP for C++. |
| 4 | + |
| 5 | +**Header-only. Simple. Expressive.** |
| 6 | + |
| 7 | +--- |
| 8 | + |
| 9 | +## Download |
| 10 | + |
| 11 | +https://vixcpp.com/registry/pkg/vix/requests |
| 12 | + |
| 13 | +--- |
| 14 | + |
| 15 | +## Overview |
| 16 | + |
| 17 | +`vix/requests` provides a simple and expressive API to perform HTTP requests in C++. |
| 18 | + |
| 19 | +It supports: |
| 20 | + |
| 21 | +- GET, POST, PUT, PATCH, DELETE |
| 22 | +- headers |
| 23 | +- query parameters |
| 24 | +- JSON body |
| 25 | +- form data |
| 26 | +- sessions |
| 27 | + |
| 28 | +It is designed to be: |
| 29 | + |
| 30 | +- minimal |
| 31 | +- predictable |
| 32 | +- easy to use |
| 33 | +- quick to integrate |
| 34 | + |
| 35 | +--- |
| 36 | + |
| 37 | +## Why vix/requests? |
| 38 | + |
| 39 | +Performing HTTP requests in C++ is often: |
| 40 | + |
| 41 | +- verbose |
| 42 | +- complex |
| 43 | +- hard to read |
| 44 | +- tightly coupled to low-level networking |
| 45 | + |
| 46 | +This leads to: |
| 47 | + |
| 48 | +- boilerplate code |
| 49 | +- poor readability |
| 50 | +- slow development |
| 51 | + |
| 52 | +`vix/requests` provides: |
| 53 | + |
| 54 | +- a clean API |
| 55 | +- simple request construction |
| 56 | +- readable code |
| 57 | + |
| 58 | +--- |
| 59 | + |
| 60 | +## Installation |
| 61 | + |
| 62 | +### Using Vix |
| 63 | + |
| 64 | +```bash |
| 65 | +vix add @vix/requests |
| 66 | +vix install |
| 67 | +``` |
| 68 | + |
| 69 | +### Manual |
| 70 | + |
| 71 | +```bash |
| 72 | +git clone https://github.com/vixcpp/requests.git |
| 73 | +``` |
| 74 | + |
| 75 | +Add `include/` to your project. |
| 76 | + |
| 77 | +--- |
| 78 | + |
| 79 | +## Requirements |
| 80 | + |
| 81 | +- `curl` must be available on the system |
| 82 | + |
| 83 | +--- |
| 84 | + |
| 85 | +## Basic Usage |
| 86 | + |
| 87 | +```cpp |
| 88 | +#include <vix/requests/requests.hpp> |
| 89 | +#include <iostream> |
| 90 | + |
| 91 | +int main() |
| 92 | +{ |
| 93 | + auto res = vix::requests::get("https://httpbin.org/get"); |
| 94 | + |
| 95 | + std::cout << res.status_code << '\n'; |
| 96 | + std::cout << res.text << '\n'; |
| 97 | +} |
| 98 | +``` |
| 99 | + |
| 100 | +--- |
| 101 | + |
| 102 | +## Query Parameters |
| 103 | + |
| 104 | +```cpp |
| 105 | +vix::requests::RequestOptions options; |
| 106 | +options.params = { |
| 107 | + {"q", "vix"}, |
| 108 | + {"page", "1"} |
| 109 | +}; |
| 110 | + |
| 111 | +auto res = vix::requests::get("https://httpbin.org/get", options); |
| 112 | +``` |
| 113 | + |
| 114 | +--- |
| 115 | + |
| 116 | +## POST Request |
| 117 | + |
| 118 | +```cpp |
| 119 | +vix::requests::RequestOptions options; |
| 120 | +options.body = "name=Gaspard&role=builder"; |
| 121 | +options.headers["Content-Type"] = "application/x-www-form-urlencoded"; |
| 122 | + |
| 123 | +auto res = vix::requests::post("https://httpbin.org/post", options); |
| 124 | +``` |
| 125 | + |
| 126 | +--- |
| 127 | + |
| 128 | +## JSON Request |
| 129 | + |
| 130 | +```cpp |
| 131 | +vix::requests::RequestOptions options; |
| 132 | +options.json = R"({ |
| 133 | + "name": "Gaspard", |
| 134 | + "project": "Vix" |
| 135 | +})"; |
| 136 | + |
| 137 | +auto res = vix::requests::post("https://httpbin.org/post", options); |
| 138 | +``` |
| 139 | + |
| 140 | +--- |
| 141 | + |
| 142 | +## Custom Headers |
| 143 | + |
| 144 | +```cpp |
| 145 | +vix::requests::RequestOptions options; |
| 146 | +options.headers["Authorization"] = "Bearer token"; |
| 147 | +options.headers["Accept"] = "application/json"; |
| 148 | + |
| 149 | +auto res = vix::requests::get("https://httpbin.org/get", options); |
| 150 | +``` |
| 151 | + |
| 152 | +--- |
| 153 | + |
| 154 | +## Response |
| 155 | + |
| 156 | +```cpp |
| 157 | +if (res.ok()) |
| 158 | +{ |
| 159 | + std::cout << res.text << '\n'; |
| 160 | +} |
| 161 | + |
| 162 | +res.raise_for_status(); |
| 163 | +``` |
| 164 | + |
| 165 | +--- |
| 166 | + |
| 167 | +## Session |
| 168 | + |
| 169 | +```cpp |
| 170 | +vix::requests::Session session; |
| 171 | + |
| 172 | +session.set_header("Accept", "application/json"); |
| 173 | +session.defaults().timeout_seconds = 10; |
| 174 | + |
| 175 | +auto res = session.get("https://httpbin.org/get"); |
| 176 | +``` |
| 177 | + |
| 178 | +--- |
| 179 | + |
| 180 | +## Execution Model |
| 181 | + |
| 182 | +- requests are executed via system `curl` |
| 183 | +- response is fully buffered |
| 184 | +- headers and body are parsed after execution |
| 185 | + |
| 186 | +--- |
| 187 | + |
| 188 | +## Complexity |
| 189 | + |
| 190 | +| Operation | Complexity | |
| 191 | +|----------|-----------| |
| 192 | +| request creation | O(1) | |
| 193 | +| execution | O(n) | |
| 194 | +| response parsing | O(n) | |
| 195 | + |
| 196 | +--- |
| 197 | + |
| 198 | +## Design Philosophy |
| 199 | + |
| 200 | +- minimal API |
| 201 | +- explicit behavior |
| 202 | +- no hidden magic |
| 203 | +- composable options |
| 204 | +- header-only simplicity |
| 205 | + |
| 206 | +--- |
| 207 | + |
| 208 | +## Tests |
| 209 | + |
| 210 | +```bash |
| 211 | +vix build |
| 212 | +vix test |
| 213 | +``` |
| 214 | + |
| 215 | +--- |
| 216 | + |
| 217 | +## License |
| 218 | + |
| 219 | +MIT License |
| 220 | +Copyright (c) Gaspard Kirira |
| 221 | + |
0 commit comments