From 3f8d8fa82ceef2c349f80199bb24a1001b367249 Mon Sep 17 00:00:00 2001 From: qicosmos Date: Tue, 21 May 2024 17:20:51 +0800 Subject: [PATCH] update doc (#587) --- README.md | 10 +++++----- lang/english/README.md | 8 ++++---- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index adc9ce73..74bae044 100644 --- a/README.md +++ b/README.md @@ -278,9 +278,9 @@ int main() { 见[example中的例子](example/main.cpp) ## 示例5:RESTful服务端路径参数设置 -本代码演示如何使用RESTful路径参数。下面设置了两个RESTful API。第一个API当访问,比如访问这样的url`http://127.0.0.1:8080/numbers/1234/test/5678`时服务器可以获取到1234和5678这两个参数,第一个RESTful API的参数是`(\d+)`是一个正则表达式表明只能参数只能为数字。获取第一个参数的代码是`req.get_matches()[1]`。因为每一个req不同所以每一个匹配到的参数都放在`request`结构体中。 +本代码演示如何使用RESTful路径参数。下面设置了两个RESTful API。第一个API当访问,比如访问这样的url`http://127.0.0.1:8080/numbers/1234/test/5678`时服务器可以获取到1234和5678这两个参数,第一个RESTful API的参数是`(\d+)`是一个正则表达式表明只能参数只能为数字。获取第一个参数的代码是`req.matches_[1]`。因为每一个req不同所以每一个匹配到的参数都放在`request`结构体中。 -同时还支持任意字符的RESTful API,即示例的第二种RESTful API`"/string/{:id}/test/{:name}"`,要获取到对应的参数使用`req.get_query_value`函数即可,其参数只能为注册的变量(如果不为依然运行但是有报错),例子中参数名是id和name,要获取id参数调用`req.get_query_value("id")`即可。示例代码运行后,当访问`http://127.0.0.1:8080/string/params_1/test/api_test`时,浏览器会返回`api_test`字符串。 +同时还支持任意字符的RESTful API,即示例的第二种RESTful API`"/string/:id/test/:name"`,要获取到对应的参数使用`req.get_query_value`函数即可,其参数只能为注册的变量(如果不为依然运行但是有报错),例子中参数名是id和name,要获取id参数调用`req.get_query_value("id")`即可。示例代码运行后,当访问`http://127.0.0.1:8080/string/params_1/test/api_test`时,浏览器会返回`api_test`字符串。 #include "cinatra.hpp" using namespace cinatra; @@ -291,14 +291,14 @@ int main() { server.set_http_handler( R"(/numbers/(\d+)/test/(\d+))", [](request &req, response &res) { - std::cout << " matches[1] is : " << req.get_matches()[1] - << " matches[2] is: " << req.get_matches()[2] << std::endl; + std::cout << " matches[1] is : " << req.matches_[1] + << " matches[2] is: " << req.matches_[2] << std::endl; res.set_status_and_content(status_type::ok, "hello world"); }); server.set_http_handler( - "/string/{:id}/test/{:name}", [](request &req, response &res) { + "/string/:id/test/:name", [](request &req, response &res) { std::string id = req.get_query_value("id"); std::cout << "id value is: " << id << std::endl; std::cout << "name value is: " << std::string(req.get_query_value("name")) << std::endl; diff --git a/lang/english/README.md b/lang/english/README.md index c7e9ae49..21ffe465 100644 --- a/lang/english/README.md +++ b/lang/english/README.md @@ -269,7 +269,7 @@ see[example](../../example/main.cpp) This code demonstrates how to use RESTful path parameters. Two RESTful APIs are set up below. When accessing the first API, such as the url `http://127.0.0.1:8080/numbers/1234/test/5678`, the server can get the two parameters of 1234 and 5678, the first RESTful API The parameter is `(\d+)`, which is a regex expression, which means that the parameter can only be a number. The code to get the first parameter is `req.get_matches ()[1]`. Because each req is different, each matched parameter is placed in the `request` structure. -At the same time, it also supports RESTful API with any character, that is, the second RESTful API in the example, and the path parameter is set to `"/string/{:id}/test/{:name}"`. To get the corresponding parameters, use the `req.get_query_value` function. The parameters can only be registered variables (if you access non-registered variables, it will run but report an error). In the example, the parameter names are id and name. To get the id parameter, call `req.get_query_value("id")` will do. After the sample code is displayed, when accessing `http://127.0.0.1:8080/string/params_1/test/api_test`, the browser will return the `api_test` string. +At the same time, it also supports RESTful API with any character, that is, the second RESTful API in the example, and the path parameter is set to `"/string/:id/test/:name"`. To get the corresponding parameters, use the `req.get_query_value` function. The parameters can only be registered variables (if you access non-registered variables, it will run but report an error). In the example, the parameter names are id and name. To get the id parameter, call `req.get_query_value("id")` will do. After the sample code is displayed, when accessing `http://127.0.0.1:8080/string/params_1/test/api_test`, the browser will return the `api_test` string. ```cpp #include "cinatra.hpp" @@ -282,14 +282,14 @@ int main() { server.set_http_handler( R"(/numbers/(\d+)/test/(\d+))", [](request &req, response &res) { - std::cout << " matches[1] is : " << req.get_matches()[1] - << " matches[2] is: " << req.get_matches()[2] << std::endl; + std::cout << " matches[1] is : " << req.matches_[1] + << " matches[2] is: " << req.matches_[2] << std::endl; res.set_status_and_content(status_type::ok, "hello world"); }); server.set_http_handler( - "/string/{:id}/test/{:name}", [](request &req, response &res) { + "/string/:id/test/:name", [](request &req, response &res) { std::string id = req.get_query_value("id"); std::cout << "id value is: " << id << std::endl; std::cout << "name value is: " << std::string(req.get_query_value("name")) << std::endl;