为什么当我在通配符参数中有双斜杠时,Gorilla mux 会进行重定向?

huangapple go评论80阅读模式
英文:

Why does Gorilla mux redirect when i have double slashes in my wildcard param?

问题

这是我的处理程序:

router.HandleFunc("/g/{gparam:.*}", MyHandler)

但是当我传递像"123://abc"这样的参数时,它会重定向并修改URL中的参数为"123:/abc"。

有没有办法避免这种情况发生?

英文:

Here my handler :

router.HandleFunc("/g/{gparam:.*}", MyHandler)

Bu when i pass something like "123://abc" as param it redirect and modify the param in url to "123:/abc".

is their a way to avoid that ?

答案1

得分: 6

这是预期的行为,并且是可配置的。默认情况下,Gorilla mux会对路径进行清理,即删除双斜杠等。你可以通过以下方式保留双斜杠:

router.SkipClean(true)

SkipClean 文档中提到:

> ...
> 当为true时,如果路由路径为"/path//to",它将保留双斜杠。这在你有一个像这样的路由时很有帮助:/fetch/http://xkcd.com/534/

英文:

It's intended behavior, and is configurable. By default, Gorilla mux will do path cleaning, i.e. removing double slash etc. for new router. You can left the double slash as is by:

router.SkipClean(true)

The SkipClean documentation says:

> ...
> When true, if the route path is "/path//to", it will remain with the double slash. This is helpful if you have a route like: /fetch/http://xkcd.com/534/

答案2

得分: 1

将斜杠作为参数可能是你的要求。你需要将斜杠转义为%2F

英文:

Having slash as param, may be your requirement. You need to escape the slashes as %2F.

huangapple
  • 本文由 发表于 2017年6月29日 15:28:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/44818867.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定