为什么GorillaMux不允许我将URL作为参数传递进去?

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

Why is GorillaMux not allowing me to pass in a URL as a parameter?

问题

我正在构建一个URL缩短器API,并尝试使用GorillaMux将URL传递给一个函数。路由处理程序如下所示:

router.HandleFunc("/new/{url}", createURL)

唯一的问题是,如果我传入:https://www.google.com(即localhost:8080/new/https://www.google.com),它会响应404页面未找到,并且URL会更改为https:/www.google.com。

我尝试在{url}部分中添加一个正则表达式模式,如下所示:{url:[a-zA-Z0-9/]+},但似乎没有起作用,并且似乎有点过度,因为我在其他地方已经检查了URL是否正确。

英文:

I'm building a URL shortener API and I'm trying to pass a URL into a function using GorillaMux. The route handler is like so:

router.HandleFunc("/new/{url}", createURL)

The only thing is, if I pass in: https://www.google.com (as in localhost:8080/new/https://www.google.com) then it responds with 404 page not found and the URL is changed to https:/www.google.com.

I've tried adding a regexp pattern in with the {url} bit like so: {url:[a-zA-Z0-9/]+} but that didn't seem to work and seems a bit overkill since I'm checking the url is correct elsewhere.

答案1

得分: 3

你需要对其进行编码,以免参数中的斜杠被误认为是URL的一部分:

localhost:8080/new/https%3A%2F%2Fwww.google.com

英文:

You need to encode it so that the slash in the parameter was not confused as a part of the url:

localhost:8080/new/https%3A%2F%2Fwww.google.com

huangapple
  • 本文由 发表于 2017年4月26日 13:50:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/43626030.html
匿名

发表评论

匿名网友

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

确定