英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论