I am a university student with an intermediate level of C++ programming experience. I would like to implement a simple REST based API for my application as quickly as possible.
I have looked at Casablanca and libWebSockets but the examples posted on their respective sites are a bit over my head. Is there any library that has more beginner oriented tutorials on creating a RESTFUL API Server in C++ ?
Note: I am aware that this question has been asked a few times in C# but the answers are over a year or two old and mostly not aimed at beginners. I hope that a new post will yield some fresh answers!
Step #1 – Enter the URL of the API in the textbox of the tool. Step #2 – Select the HTTP method used for this API (GET, POST, PATCH, etc). Step #3 – Enter any headers if they are required in the Headers textbox. Step #4 – Pass the request body of the API in a key-value pair.
Under REST architecture, the client and server can only interact in one way: The client sends a request to the server, then the server sends a response back to the client. Servers cannot make requests and clients cannot respond — all interactions are initiated by the client.
Using multiple threads: The REST API can use multiple threads to handle multiple client requests. This means that each request will be processed on a separate thread, and the REST API can process multiple requests at the same time. Using a queue: The REST API can use a queue to store incoming requests from clients.
Restbed offers asynchronous client/server capabilities via ASIO and C++11. We have lots of examples and documentation will be available shortly for those who don't like reading header files.
#include <memory>
#include <cstdlib>
#include <restbed>
using namespace std;
using namespace restbed;
void post_method_handler( const shared_ptr< Session > session )
{
const auto request = session->get_request( );
int content_length = 0;
request->get_header( "Content-Length", content_length );
session->fetch( content_length, [ ]( const shared_ptr< Session > session, const Bytes & body )
{
fprintf( stdout, "%.*s\n", ( int ) body.size( ), body.data( ) );
session->close( OK, "Hello, World!", { { "Content-Length", "13" } } );
} );
}
int main( const int, const char** )
{
auto resource = make_shared< Resource >( );
resource->set_path( "/resource" );
resource->set_method_handler( "POST", post_method_handler );
auto settings = make_shared< Settings >( );
settings->set_port( 1984 );
settings->set_default_header( "Connection", "close" );
Service service;
service.publish( resource );
service.start( settings );
return EXIT_SUCCESS;
}
The next major feature will allow the ability to dependency inject application layers.
auto settings = make_shared< Settings >( );
Service service;
service.add_application_layer( http_10_instance );
service.add_application_layer( http_11_instance );
service.add_application_layer( http2_instance );
service.add_application_layer( spdy_instance );
service.start( settings );
http://pistache.io/ looks a good and modern to me. The hello world is just 9 lines long.
ngrest is a simple REST framework with epoll, codegeneration, command line tool, extensions and other sugar.
It's easy in use and suitable for beginners; written on C++11 and uses CMake for build.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With