我想在我的应用程序上公开REST API,使用Mongoose Web服务器并为不同的查询提供处理程序.
查询的一个例子就是这样(我现在只使用GET,其余的HTTP动词将在以后出现):
GET /items -> returns a list of all items in JSON
GET /item/by/handle/123456789 -> returns item that has handle 123456789
GET /item/by/name/My%20Item -> returns item(s) that have the name "My Item"
Run Code Online (Sandbox Code Playgroud)
我很好奇的是我应该如何实现这些查询的解析.我可以很容易地解析第一个,因为它只是一个问题if( query.getURI() == "/items") return ....
但是对于接下来的两个查询,我必须以std::完全不同的方式操纵字符串,使用一些std::string::find()魔法和偏移来获得参数.
作为示例,这是我对第二个查询的实现:
size_t position = std::string::npos;
std::string path = "/item/by/handle/";
if( (position = query.getURI().find(path) ) != std::string::npos )
{
std::string argument = query.getURI().substr( position + path.size() );
// now parse the argument …Run Code Online (Sandbox Code Playgroud)