Skip to content

Commit 5cf1bda

Browse files
committed
feat(requests): initial release of header-only HTTP client
- add simple HTTP API (get, post, put, patch, delete, head) - add RequestOptions (headers, params, json, form, auth, timeout) - add Response API (status_code, headers, text, ok, raise_for_status) - add Session support with default options - add query builder and url encoding utilities - add examples and basic tests - add CMake configuration (registry-safe, header-only) - add vix.json package definition - add README with usage and design principles
1 parent a677395 commit 5cf1bda

13 files changed

Lines changed: 1682 additions & 37 deletions

File tree

.gitignore

Lines changed: 12 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,18 @@
1-
# Prerequisites
2-
*.d
3-
4-
# Compiled Object files
5-
*.slo
6-
*.lo
1+
/build*/
2+
out/
3+
.vscode/
4+
.idea/
75
*.o
86
*.obj
9-
10-
# Precompiled Headers
11-
*.gch
12-
*.pch
13-
14-
# Linker files
15-
*.ilk
16-
17-
# Debugger Files
18-
*.pdb
19-
20-
# Compiled Dynamic libraries
7+
*.a
8+
*.lib
219
*.so
2210
*.dylib
2311
*.dll
24-
25-
# Fortran module files
26-
*.mod
27-
*.smod
28-
29-
# Compiled Static libraries
30-
*.lai
31-
*.la
32-
*.a
33-
*.lib
34-
35-
# Executables
3612
*.exe
37-
*.out
38-
*.app
39-
40-
# debug information files
41-
*.dwo
13+
compile_commands.json
14+
CMakeFiles/
15+
CMakeCache.txt
16+
cmake-build-*/
17+
cmd.md
18+
.vix/

CMakeLists.txt

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
cmake_minimum_required(VERSION 3.20)
2+
project(requests VERSION 0.1.0 LANGUAGES CXX)
3+
4+
add_library(requests INTERFACE)
5+
add_library(requests::requests ALIAS requests)
6+
7+
target_compile_features(requests INTERFACE cxx_std_20)
8+
9+
target_include_directories(requests
10+
INTERFACE
11+
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
12+
$<INSTALL_INTERFACE:include>
13+
)
14+
15+
if(MSVC)
16+
target_compile_options(requests INTERFACE /W4 /permissive-)
17+
else()
18+
target_compile_options(requests INTERFACE -Wall -Wextra -Wpedantic)
19+
endif()
20+
21+
option(REQUESTS_BUILD_TESTS "Build requests tests" OFF)
22+
option(REQUESTS_BUILD_EXAMPLES "Build requests examples" OFF)
23+
24+
include(CTest)
25+
26+
if(REQUESTS_BUILD_TESTS)
27+
enable_testing()
28+
29+
add_executable(requests_test_basic tests/test_basic.cpp)
30+
target_link_libraries(requests_test_basic PRIVATE requests::requests)
31+
32+
add_test(NAME requests.basic COMMAND requests_test_basic)
33+
endif()
34+
35+
if(REQUESTS_BUILD_EXAMPLES)
36+
add_subdirectory(examples)
37+
endif()

CMakePresets.json

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{
2+
"version": 6,
3+
"configurePresets": [
4+
{
5+
"name": "dev-ninja",
6+
"displayName": "Dev (Ninja, Debug)",
7+
"generator": "Ninja",
8+
"binaryDir": "build-ninja",
9+
"cacheVariables": {
10+
"CMAKE_BUILD_TYPE": "Debug",
11+
"CMAKE_EXPORT_COMPILE_COMMANDS": "ON"
12+
}
13+
},
14+
{
15+
"name": "release",
16+
"displayName": "Release (Ninja, Release)",
17+
"generator": "Ninja",
18+
"binaryDir": "build-release",
19+
"cacheVariables": {
20+
"CMAKE_BUILD_TYPE": "Release",
21+
"CMAKE_EXPORT_COMPILE_COMMANDS": "ON"
22+
}
23+
},
24+
{
25+
"name": "dev-msvc",
26+
"displayName": "Dev (MSVC, Release)",
27+
"generator": "Visual Studio 17 2022",
28+
"architecture": { "value": "x64" },
29+
"binaryDir": "build-msvc",
30+
"cacheVariables": {
31+
"CMAKE_CONFIGURATION_TYPES": "Release"
32+
}
33+
}
34+
],
35+
"buildPresets": [
36+
{ "name": "build-ninja", "displayName": "Build (ALL, Ninja Debug)", "configurePreset": "dev-ninja" },
37+
{ "name": "build-release", "displayName": "Build (ALL, Ninja Release)", "configurePreset": "release" },
38+
{ "name": "build-msvc", "displayName": "Build (ALL, MSVC)", "configurePreset": "dev-msvc", "configuration": "Release" }
39+
]
40+
}

README.md

Lines changed: 221 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,221 @@
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+

examples/01_get.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* @file 01_get.cpp
3+
* @brief Basic GET request example
4+
*/
5+
6+
#include <iostream>
7+
#include <vix/requests/requests.hpp>
8+
9+
int main()
10+
{
11+
try
12+
{
13+
auto response = vix::requests::get("https://httpbin.org/get");
14+
15+
std::cout << "Status: " << response.status_code << '\n';
16+
std::cout << "OK: " << std::boolalpha << response.ok() << '\n';
17+
std::cout << "Body:\n"
18+
<< response.text << '\n';
19+
20+
return 0;
21+
}
22+
catch (const std::exception &e)
23+
{
24+
std::cerr << "Request failed: " << e.what() << '\n';
25+
return 1;
26+
}
27+
}

0 commit comments

Comments
 (0)