如何使用c ++语言和JSON Parser创建Restful Web服务

 手机用户2502902913 发布于 2022-12-31 12:39

我正在使用嵌入式Linux,我希望在我的Linux自定义板上运行Restful Web服务.

我的目标是向/从Web服务器(httpd服务器)发送/接收数据(采用JSON格式).

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

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

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

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

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

有没有人有关于如何使用C++语言创建Restful Web Services的想法或示例?

欢迎任何有关Restful的帮助.

有没有人知道ffead及其功能是否满足我的Restful Web Services?

1 个回答
  • 除了JSON解析器之外,Restbed可以满足您的要求.但是,将解决方案与已有的许多C++ JSON实现之一相结合,只需要很少的工作.

    #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框架

    卡萨布兰卡

    CPP-入Netlib

    RESTful Mapper

    简单的REST

    谷歌是你的朋友.

    替代JSON框架

    LibJSON

    RapidJSON

    JSONMe

    JSON ++

    JSON-CPP

    谷歌是你的朋友.

    2022-12-31 12:40 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有