Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/echo-devim/nicehttp
Small HTTP REST API client/server framework in C++23
https://github.com/echo-devim/nicehttp
client cpp cpp23 http rest-api server
Last synced: 18 days ago
JSON representation
Small HTTP REST API client/server framework in C++23
- Host: GitHub
- URL: https://github.com/echo-devim/nicehttp
- Owner: echo-devim
- License: bsd-3-clause
- Created: 2024-10-10T15:53:11.000Z (about 1 month ago)
- Default Branch: main
- Last Pushed: 2024-10-24T06:35:43.000Z (24 days ago)
- Last Synced: 2024-10-25T01:42:54.671Z (23 days ago)
- Topics: client, cpp, cpp23, http, rest-api, server
- Language: C++
- Homepage:
- Size: 21.5 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# NiceHTTP
Small cross-platform HTTP REST API client/server framework written as an exercise to learn modern C++23.
The framework supports only HTTP 1.1 and can be used to deploy simple rest api services.
This project is made for inter-communication among applications using HTTP protocol. The code is very simple and hackable, thus you can customize it for your needs.
Features:
- Cross-Platform (supports Linux/Windows)
- Supports only HTTP/1.1 protocol
- Supports authentication
- Server multi-threaded
- single file header to include
- very easy and fast
- designed to exchange json messages## Server example
```c++
//handle /test/ endpoint
http::Response handle_test(const http::Request &req) {
// here you can parse the request, including parameters
cout << "Requested uri: " << req.uri << endl;
for (const auto& p : const_cast(req).getParams()) {
std::cout << "key '" << p.first << "' has value '" << p.second << "'" << std::endl;
}
// generate response
map headers;
string body = "{\"status\": \"OK\"}";
http::Response resp {200, "OK", PROTO_HTTP1, headers, true, body.length(), body};
return resp;
}void main() {
NiceHTTP nhttp;
// Route supports regex
Route r {"GET", "/test/[0-9]", handle_test, "apptoken123"};
nhttp.getRouter().add(r);
nhttp.start("127.0.0.1", 8090);
}
```## Client example
```c++
void main() {
NiceHTTP nhttp;
map headers;
// Add auth app token
headers.insert({"Authorization", "apptoken123"});
http::Request req {"GET", "/test/2?id=45&user=admin", PROTO_HTTP1, headers, false, 0, ""};
http::Response r;
r = nhttp.request(req, "localhost", 8090);
cout << r.toString(false) << endl;
}
```# Compile
Use the following commands to compile the project for Linux.
Demo server compilation:
```sh
g++ -D server -D NICEHTTP_VERBOSE -std=c++23 -o nhttpsrv main.cpp
```
Demo client compilation:
```sh
g++ -D client -std=c++23 -o nhttpcl main.cpp
```Use the following commands to compile the project for Windows.
Demo server compilation:
```sh
x86_64-w64-mingw32-g++ -D server -D NICEHTTP_VERBOSE -std=c++23 -static -fno-rtti -o nhttpsrv.exe main.cpp -lws2_32
```Demo client compilation:
```sh
x86_64-w64-mingw32-g++ -D client -std=c++23 -static -fno-rtti -o nhttpcl.exe main.cpp -lws2_32
```