C++ 如何实现 REST API 服务器?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32017457/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to implement a REST API Server?
提问by MGItkin
I am a university student with an intermediate level of C++ programming experience. I would like to implement a simple REST based API for my application as quickly as possible.
我是一名具有中级 C++ 编程经验的大学生。我想尽快为我的应用程序实现一个简单的基于 REST 的 API。
I have looked at Casablancaand libWebSocketsbut the examples posted on their respective sites are a bit over my head. Is there any library that has more beginner oriented tutorials on creating a RESTFUL API Server in C++ ?
我查看了Casablanca和libWebSockets,但他们各自网站上发布的示例有点超出我的理解。是否有任何库提供更多关于在 C++ 中创建 RESTFUL API 服务器的面向初学者的教程?
Note:I am aware that this question has been asked a few times in C# but the answers are over a year or two old and mostly not aimed at beginners. I hope that a new post will yield some fresh answers!
注意:我知道这个问题在 C# 中被问过几次,但答案已经有一两年多的历史了,而且大多不是针对初学者的。我希望一个新的帖子会产生一些新的答案!
采纳答案by vivek.s.patel
Hey I also was new to the whole API game not too long ago. I found that deploying an ASP.NET Web APIwith Visual Studio was a great way to start. The template provided by VS (I'm using 2013) makes it really easy to create your own controllers.
嘿,不久前我也是整个 API 游戏的新手。我发现使用 Visual Studio部署ASP.NET Web API是一个很好的开始方式。VS 提供的模板(我使用的是 2013)使创建自己的控制器变得非常容易。
If you look up a couple tutorials on HTTP methods, you can really get the hang of molding your controller(s) to your needs. They map well to the CRUD operations which I'm sure you're looking to perform.
如果您查阅一些关于 HTTP 方法的教程,您就可以真正掌握根据需要塑造控制器的窍门。它们很好地映射到我确定您要执行的 CRUD 操作。
You should also be able to find a library in C++ that will allow you to call each of your controller methods and pass/receive serialized JSON/XML objects. Hope this helped, good luck! :)
您还应该能够在 C++ 中找到一个库,它允许您调用每个控制器方法并传递/接收序列化的 JSON/XML 对象。希望这有帮助,祝你好运!:)
回答by Ben Crowhurst
Restbedoffers asynchronous client/server capabilities via ASIOand C++11. We have lots of examplesand documentation will be available shortly for those who don't like reading header files.
Restbed通过ASIO和 C++11提供异步客户端/服务器功能。我们有很多示例,很快就会为那些不喜欢阅读头文件的人提供文档。
#include <memory>
#include <cstdlib>
#include <restbed>
using namespace std;
using namespace restbed;
void post_method_handler( const shared_ptr< Session > session )
{
const auto request = session->get_request( );
int content_length = 0;
request->get_header( "Content-Length", content_length );
session->fetch( content_length, [ ]( const shared_ptr< Session > session, const Bytes & body )
{
fprintf( stdout, "%.*s\n", ( int ) body.size( ), body.data( ) );
session->close( OK, "Hello, World!", { { "Content-Length", "13" } } );
} );
}
int main( const int, const char** )
{
auto resource = make_shared< Resource >( );
resource->set_path( "/resource" );
resource->set_method_handler( "POST", post_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;
}
The next major feature will allow the ability to dependency inject application layers.
下一个主要功能将允许依赖注入应用层的能力。
auto settings = make_shared< Settings >( );
Service service;
service.add_application_layer( http_10_instance );
service.add_application_layer( http_11_instance );
service.add_application_layer( http2_instance );
service.add_application_layer( spdy_instance );
service.start( settings );
回答by Djeefther Souza
http://pistache.io/looks a good and modern to me. The hello world is just 9 lines long.
http://pistache.io/对我来说看起来很好很现代。你好世界只有 9 行。
回答by loentar
回答by Joshua Sprague
i'm not aware of any popular c/c++ rest frameworks to easily achieve this.
我不知道有任何流行的 c/c++ 休息框架可以轻松实现这一点。
generally, RESTful frameworks are more popular for higher level languages such as java/.NET/javascript/python/etc...
一般来说,RESTful 框架在更高级的语言中更受欢迎,例如 java/.NET/javascript/python/etc...
implementing a RESTful interface without a framework is possible, but it really isn't ideal.
在没有框架的情况下实现 RESTful 接口是可能的,但它确实不理想。