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

早过忘川 提交于 2019-12-12 06:17:42

问题


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:


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


来源:https://stackoverflow.com/questions/43626030/why-is-gorillamux-not-allowing-me-to-pass-in-a-url-as-a-parameter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!