智能设备REST开发

开源库资源

Microsoft REST SDK

restbed

restclient-cpp

代码示例

基于 cpprest 的服务器核心代码

CRestServer::CRestServer()
    : m_listener(U("http://0.0.0.0:8090"))
{
    TRACEPOINT();
    m_listener.support(web::http::methods::GET, std::bind(&CRestServer::handle_get_or_post, this, std::placeholders::_1));
    m_listener.support(web::http::methods::POST, std::bind(&CRestServer::handle_get_or_post, this, std::placeholders::_1));
}

CRestServer::~CRestServer()
{
    TRACEPOINT();
    Stop();
}

bool CRestServer::Start()
{
    TRACEPOINT();
    m_listener.open();
    return true;
}

bool CRestServer::Stop()
{
    TRACEPOINT();
    m_listener.close().wait();
    return true;
}

void CRestServer::handle_get_or_post(web::http::http_request message)
{
    ucout << "Method: " << message.method() << std::endl;
    ucout << "URI: " << web::http::uri::decode(message.relative_uri().path()) << std::endl;
    ucout << "Query: " << web::http::uri::decode(message.relative_uri().query()) << std::endl << std::endl;
    message.reply(web::http::status_codes::OK, "ACCEPTED");
}

参考链接

RESTful API 设计指南

RESTful API 设计最佳实践

c++并行计算库TBB和PPL的基本用法



blog comments powered by Disqus

Published

16 January 2017

Tags