dongya8378 2019-09-03 06:01
浏览 54
已采纳

go / mux中的第一个匹配项后停止匹配

I have the following router:

func testHttp() {

    r := mux.NewRouter()
    s := r.PathPrefix("/cmd").Subrouter()
    s.HandleFunc("/{cmd}", cmd)
    http.Handle("/", r)

    http.ListenAndServe(":8090", nil)
}

When I make a request such as localhost:8090/cmd/ls -la then the cmd handler is executed correctly. However If I pass in something like localhost:8090/cmd/ls -la /home/foo I get a 404. This implies that gorilla/mux keeps matching if it detects a /. So how do I configure it such that everything after a particular matched path (in this case /cmd/) is considered a parameter to the path? Presumably I could just resort to using GET parameters but can this be achieved if using the /rest-endpoint/params-containing/ as well ?

  • 写回答

1条回答

  • doujieluo5875 2019-09-03 06:12
    关注

    / is a directory separator for URLs, so what mux is doing is correct that it is separating and matching segments delimited by /. If you want to pass ls -la /home/foo as a path parameter, then you should escape the / characters, and write %2F. Then mux will run correctly.

    Another option can be to install a handler to PathPrefix /cmd/. Then you have to parse the remaining part of the path in your handler, but you can treat '/' the way you want.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 求会做山景bo1048b2程序的 直播声卡用
  • ¥15 51单片机与数码管实现电子琴
  • ¥15 h3.6m 人类行为预测论文复现
  • ¥50 wordpress项目注册报失败刷新后其实是成功状态,请求排查原因
  • ¥20 linxu服务器僵尸进程不释放,代码如何修改?
  • ¥15 pycharm激活不成功
  • ¥40 如果update 一个列名为参数的value
  • ¥15 基于51单片机的水位检测系统设计中LCD1602一直不显示
  • ¥15 OCS2安装出现问题,请大家给点意见
  • ¥15 ros小车启动launch文件报错