49

I am working on Embedded Linux and I want Restful web services to run on my Linux Custom Board.

My objective is to send/receive data (in JSON format) to/from web server (httpd server).

Also, I want to create that Restful Web services using C++ language.

Please see below idea about need of Restful Web Services for my Linux Custom Board.

  1. First, I will send HTTP request with JSON format data through httpd server which is running on my linux board.

  2. Then, I want to create one binary or server which implements this Restful Web Services in c++ language which is used to handle HTTP request.

  3. Then, This C++ binary will send response back to httpd server for display purpose over web browser.

Does anyone have idea or example about how to create Restful Web Services using C++ language?

Any help on Restful is welcome.

Does any one has idea about ffead and its functionalities which fulfills my Restful Web Services or not?

9 Answers 9

39

Restbed can address your requirements with the exception of a JSON parser. However, combining a solution with one of the many C++ JSON implementations already available requires very little work.

#include <memory>
#include <string>
#include <cstdlib>
#include <sstream>
#include <jsonbox.h>
#include <restbed>

using namespace std;
using namespace restbed;

void get_method_handler( const shared_ptr< Session > session )
{
    const auto request = session->get_request( );

    size_t content_length = request->get_header( "Content-Length", 0 );

    session->fetch( content_length, [ ]( const shared_ptr< Session >& session, const Bytes& body )
    {
        JsonBox::Value json;
        json.loadFromString( string( body.begin( ), body.end( ) ) );

        //perform awesome solutions logic...

        stringstream stream;
        json.writeToStream( stream );
        string response_body = stream.str( );

        session->close( OK, response_body, { { "Content-Length", ::to_string( response_body.length( ) }, { "Content-Type": "application/json" } } );
    } );
}

int main( const int, const char** )
{
    auto resource = make_shared< Resource >( );
    resource->set_path( "/resource" );
    resource->set_method_handler( "GET", get_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;
}

Alternative RESTful frameworks

Alternative JSON frameworks

Sign up to request clarification or add additional context in comments.

9 Comments

If you are developing over Linux... be careful, because SSL is just implemented over windows systems. If it is not a problem, in my opinion casablanca is the best API Rest framework in C++.
@GutiMac Restbed has had SSL working on BSD, Mac, Linux, and Windows systems since version 2.5. If you're having difficulty understanding its use, please do raise an issue on the project page and we'd be happy to help you.
Yes, casablanca is a really good framework... It is really complet but the SSL is a restriction for my software and for this reason I changed of framework. Yes, I have played with restbed... but the license "AGPL" doesn't work for me.... So I find as best approach "POCO C++ Network library" in my opinion it is really useful and you can create a appy rest with SSL easily. I woul like to know your opinion about it.
C++ REST SDK supports Linux, OS X, Unix, iOS, and Android in addition to Windows. If it was renamed from Casablanca, then it must have be re-written to support additional platforms. Those comments stating that it only supports Windows appear to be no longer correct.
As of 2021 C++ REST SDK can be considered dead! its no more actively developed, so users must think twice before using it.
|
6

I know this is late, but something new has come up a year or two ago.

If you're into hardcore asynchronous programming for high-performance, you can consider boost::beast. It's a layer above boost::asio (the generic tcp/udp and i/o library) that has both http(s) and websocket servers/clients.

Keep in mind that these are ideal for performance and full freedom in multithreading (you can literally run your server on thousands of threads with almost perfect caching if you have a server that can take it), but they have a steep learning-curve. Do this only if you need to have absolute control over everything!

Comments

6

I'm using oatpp in combination with nlohmann JSON. Though you may get away with purely oatpp as it has built-in JSON handling features. Please, see a step by step guide.

1 Comment

How did you integrate nlohmann/json with oatpp? Do you transfer the payload as an oatpp::String in the DTO and then parse it with nlohmann/json? Or do you directly include the nlohmann/json object as a member of the DTO?
3

If you are building a RESTful service client, you should consider a library such as Casablanca (which is maintained by Microsoft and is a cross-platform solution for accessing RESTful APIs) https://github.com/Microsoft/cpprestsdk.

Otherwise you could also use libcurl https://curl.haxx.se/libcurl

There are C++ bindings for curl. Depending on your board, libcurl might already be installed. All you have to do would be to use the C++ bindings.

Both libraries handle http/https. libcurl does not provide a json parser but you could easily combine it with any C++ json parser available. Casablanca is complete and built on an asynchronous model. It relies however on the Boost libraries. Nevertheless I have used on an Intel Edison board with success.

Comments

2

For send/receive data in JSON format, try jsoncpp

Comments

2

Use an embedded web server, such as Mongoose, CivetWeb or NXWeb. (see this post for more details)

Generally these are trivial to embed in your application, then you only need a JSON library to parse/create JSON data in the web server route handlers. REST is, after all, only HTTP requests so this is easy to implement using one of the above.

2 Comments

All above Web servers are separate web servers or not. Can it be embedded with httpd web server or not? Because I just want to use httpd web server to create Restful Web Service using c++ language and JSON Parser to parse HTTP request from JSON to c++ and vice-versa.
Not. mongoose is 1 c file that you build into your project. Add 2 lines of code to initialize and start it, and your program suddenly is listening and serving http requests. Add a function to respond to each URL route and you're done. You won't need to use a separate httpd service.
2

The minimal web service capable of using SHTML would include Open SSL..

Here is an example of a simple C++ SHTML Server using TCP/IP calls no framework other than OpenSSL

Here is a pretty comprehensive list of Restful Frameworks by language like 300 of them..

As of writing this answer searching that more comprehensive list for "C++" yields more than 60 entries.

Comments

1

Probably your best bet is to use FastCGI to create a module to interface with your web server. This should prevent you from having to implement your own HTTP server.

2 Comments

Do you have any link about how to create Restful web services using FastCGI?
This is just one piece of the puzzle. So read the specs and implement the stuff yourself.
1

There are some frameworks like CppCMS that embed their own HTTP server, so you might not need something as heavy as Apache httpd.

I'm assuming your REST service will not be under heavy load.

Also, CppCMS supports JSON out of the box, see http://cppcms.com/wikipp/en/page/cppcms_1x_json.

5 Comments

CppCMS is just for c++ and JSON Parsing. I need to create Restful Web Services which reads that HTTP request and send response back to HTTP. And I also want to create that Restful services into c++ language. So, HTTP request comes in JSON format. Then it is converted into c++ and handled from Restful Web services. Then it again converted into JSON format and send back that response to httpd server for output display.
@RiteshPrajapati "CppCMS is just for c++ and JSON Parsing" I am not sure what you mean. CppCMS is a full-stack web framework which stated design goal seems to match your needs, see cppcms.com/wikipp/en/page/when_to_use_cppcms...
Does it supports as module with apache web browser (httpd)?
While CppCMS can be used as its own webserver it can also be used to create FastCGI binaries that can interact with the Apache Web Server through mod_fastcgi or mod_fcgid.
Do you have any idea about ffead server and its functionalities which is also c++ based Restful Web Development Services and supports JSON Parser also?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.