An open API service indexing awesome lists of open source software.

https://github.com/edenreich/http-component

An easy to use native component for interacting with the http layer written in C++
https://github.com/edenreich/http-component

cmake cpp cpp-component cpp17 http http-client http-server request-handler response-parsing unix-socket winsock2

Last synced: 3 months ago
JSON representation

An easy to use native component for interacting with the http layer written in C++

Awesome Lists containing this project

README

        


GitHub Actions status

# HTTP Component

An easy to use component for interacting with the http layer written in C++

## Usage

```cpp
// main.cpp
#include
#include
#include
#include

#include

using namespace Http;

int main(int argc, char const *argv[])
{
Server server;

server.bind("0.0.0.0");

server.listen(8080);

server.onConnection([](Interfaces::ClientInterface * client) {
Interfaces::MessageInterface * requestMessage = client->getRequest()->getMessage();
std::stringstream responseMessage;

std::string json = R"JSON(
[
{
"id": 1,
"name": "Pizza",
"description": "lorem ipsum"
},
{
"id": 2,
"name": "Cola",
"description": "lorem ipsum"
},
{
"id": 3,
"name": "Hamburger",
"description": "lorem ipsum"
}
]
)JSON";

responseMessage << "HTTP/1.1 200 OK\r\n";
responseMessage << "Content-Type: application/json\r\n";
responseMessage << "Content-Length: " << json.length() << "\r\n";
responseMessage << "Connection: close\r\n";
responseMessage << "\r\n";
responseMessage << "\r\n";
responseMessage << json;

return new Response(new Message(responseMessage));
});

return 0;
}
```

## Target

This project targets Linux, Windows and macOS