我正在使用嵌入式
Linux,我希望Restful Web服务在我的Linux定制板上运行.
我的目标是向/从Web服务器(httpd服务器)发送/接收数据(以JSON格式).
此外,我想使用c语言创建Restful Web服务.
请参阅以下关于我的Linux定制板需要Restful Web服务的想法.
1)首先,我将通过在我的linux板上运行的httpd服务器发送具有JSON格式数据的HTTP请求.
2)然后,我想创建一个二进制或服务器,它实现了用于处理HTTP请求的c语言的Restful Web Services.
3)然后,该c二进制文件将通过Web浏览器将响应发送回httpd服务器进行显示.
有没有人有关于如何使用c语言创建Restful Web Services的想法或示例?
欢迎任何有关Restful的帮助.
有没有人有关于ffead及其功能的想法来满足我的Restful Web Services?
解决方法
Restbed可以满足您的需求,但JSON解析器除外.然而,将解决方案与已经可用的许多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框架
> Casablanca
> CPP-Netlib
> RESTful Mapper
> Simple REST
> Google是你的朋友.
可选的JSON框架
> LibJSON
> RapidJSON
> JSONMe
> JSON++
> JSON-CPP> Google是你的朋友.