Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

路由的占位符解析和查询参数的解析覆盖问题 #573

Open
zixiai opened this issue Jun 20, 2024 · 4 comments
Open

路由的占位符解析和查询参数的解析覆盖问题 #573

zixiai opened this issue Jun 20, 2024 · 4 comments

Comments

@zixiai
Copy link

zixiai commented Jun 20, 2024

router.cpp示例中:

// RESTful API: /group/:group_name/user/:user_id
// curl -v -X DELETE http://ip:port/group/test/user/123
router.Delete("/group/:group_name/user/:user_id", Handler::restful);
// router.Delete("/group/{group_name}/user/{user_id}", Handler::restful);

这里路由占位符的解析是在Handler::restful中调用了HttpContext的param,即HttpRequest的GetParam:

int Handler::restful(const HttpContextPtr& ctx) {
// RESTful /:field/ => HttpRequest::query_params
// path=/group/:group_name/user/:user_id
std::string group_name = ctx->param("group_name");
std::string user_id = ctx->param("user_id");
ctx->set("group_name", group_name);
ctx->set("user_id", user_id);
response_status(ctx, 0, "OK");
return 200;

这个方法是查询参数的解析,这样合在一起会导致这里占位符的解析覆盖了查询参数的解析,如对于/api/test/{str},会导致 GET http://ip:port/api/test/xxx?str=yyy 后,服务器获取查询参数param("str")时,xxx会覆盖yyy

@ithewei
Copy link
Owner

ithewei commented Jun 20, 2024

为了使用简单,所以不管是url里的还是query_string里的参数都是解析后保持在query_params里,通过GetParam获取,建议使用不同的参数名区分下。

@zixiai
Copy link
Author

zixiai commented Jun 20, 2024

好的,不过个人认为还是做个区分比较好,后面加个getRoute的接口可以考虑吗?

@ithewei
Copy link
Owner

ithewei commented Jun 20, 2024

为了前向兼容性暂不考虑,如果是你个人fork后修改,我推荐命名GetUrlParam和GetQueryParam

@zixiai
Copy link
Author

zixiai commented Jun 20, 2024

好的,如果是为前向兼容考虑的话,个人认为可以给路由和查询参数新建两个接口,其值也分别新建成员变量存储,旧的GetParam接口和query_params声明不推荐使用

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants