如何使用C ++语言和JSON解析器创建Restful Web Services [英] How to Create Restful Web Services using c++ language and JSON Parser

查看:160
本文介绍了如何使用C ++语言和JSON解析器创建Restful Web Services的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在嵌入式Linux上工作,我希望Restful Web服务能够在我的Linux自定义板上运行.

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

我的目标是与Web服务器(httpd服务器)之间收发数据(JSON格式).

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

此外,我想使用C ++语言创建Restful Web服务.

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

请参阅以下有关Linux自定义板需要Restful Web服务的想法.

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

  1. 首先,我将通过linux板上运行的httpd服务器发送具有JSON格式数据的HTTP请求.

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

然后,我想创建一个二进制文件或服务器,该二进制文件或服务器以用于处理HTTP请求的c ++语言实现此Restful Web Services.

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.

然后,此C ++二进制文件会将响应发送回httpd服务器,以通过Web浏览器进行显示.

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

有人对使用C ++语言创建Restful Web Services有想法或示例吗?

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

欢迎任何有关Restful的帮助.

Any help on Restful is welcome.

是否有人对 ffead 及其功能实现我的Restful Web Services有想法?

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

推荐答案

Restbed 可以满足您的要求JSON解析器除外.但是,将解决方案与众多可用的C ++ JSON 实现之一结合起来,所需的工作很少.

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;
}

其他RESTful框架

Alternative RESTful frameworks

  • Casablanca C++ REST SDK (edit: project moved & renamed)
  • CPP-Netlib
  • RESTful Mapper
  • Simple REST (edit: no longer exist)
  • Google is your friend.

备用JSON框架

  • LibJSON
  • RapidJSON
  • JSONMe
  • JSON++
  • JSON-CPP
  • Google is your friend.

这篇关于如何使用C ++语言和JSON解析器创建Restful Web Services的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆