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.

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

报告相同问题?

悬赏问题

  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?