为什么GorillaMux不允许我传入URL作为参数? [英] Why is GorillaMux not allowing me to pass in a URL as a parameter?
问题描述
我正在构建一个URL shortener API,我试图使用GorillaMux将一个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)
唯一的是,如果我传入: https://www.google.com (如localhost:8080 / new / https://www.google.com ),那么它会以 404页未找到
的方式进行响应,并且该URL将更改为https:/www.google.com 。
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.
我尝试在{url}位中添加正则表达式模式,如下所示:{url:[a-zA-Z0-9 /] +}但这似乎并没有工作,似乎有点矫枉过正,因为我检查的网址在其他地方是正确的。
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.
推荐答案
以便对参数中的斜线不会混淆为url的一部分:
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
这篇关于为什么GorillaMux不允许我传入URL作为参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!