如何使用c语言和JSON解析器创建Restful Web Services

前端之家收集整理的这篇文章主要介绍了如何使用c语言和JSON解析器创建Restful Web Services前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用嵌入式 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实现之一组合需要很少的工作.
  1. #include <memory>
  2. #include <string>
  3. #include <cstdlib>
  4. #include <sstream>
  5. #include <jsonBox.h>
  6. #include <restbed>
  7.  
  8. using namespace std;
  9. using namespace restbed;
  10.  
  11. void get_method_handler( const shared_ptr< Session > session )
  12. {
  13. const auto request = session->get_request( );
  14.  
  15. size_t content_length = request->get_header( "Content-Length",0 );
  16.  
  17. session->fetch( content_length,[ ]( const shared_ptr< Session >& session,const Bytes& body )
  18. {
  19. JsonBox::Value json;
  20. json.loadFromString( string( body.begin( ),body.end( ) ) );
  21.  
  22. //perform awesome solutions logic...
  23.  
  24. stringstream stream;
  25. json.writeToStream( stream );
  26. string response_body = stream.str( );
  27.  
  28. session->close( OK,response_body,{ { "Content-Length",::to_string( response_body.length( ) },{ "Content-Type": "application/json" } } );
  29. } );
  30. }
  31.  
  32. int main( const int,const char** )
  33. {
  34. auto resource = make_shared< Resource >( );
  35. resource->set_path( "/resource" );
  36. resource->set_method_handler( "GET",get_method_handler );
  37.  
  38. auto settings = make_shared< Settings >( );
  39. settings->set_port( 1984 );
  40. settings->set_default_header( "Connection","close" );
  41.  
  42. Service service;
  43. service.publish( resource );
  44. service.start( settings );
  45.  
  46. return EXIT_SUCCESS;
  47. }

其他RESTful框架

> Casablanca
> CPP-Netlib
> RESTful Mapper
> Simple REST
> Google是你的朋友.

可选的JSON框架

> LibJSON
> RapidJSON
> JSONMe
> JSON++
> JSON-CPP> Google是你的朋友.

猜你在找的JavaScript相关文章