dou426098 2017-04-26 13:30
浏览 698
已采纳

如何在Golang路由中将URL作为参数传递?

I'm trying to pass a URL as a parameter in Golang, and I haven't been able to find a solution in all of the tutorials I've looked at. The problem is that I can only get the url to return minus a crucial forward slash.

My handler looks like this:

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

So the request would look like:

www.myapp.heroku.com/new/https://www.google.com

However, the url that I results is missing a slash:

http:/www.google.com

I sure it's probably got something to do with RFC3986, but is there a way to pass in the url as it is?

  • 写回答

2条回答 默认 最新

  • drllqg2903 2018-03-14 02:01
    关注

    This particular behavior in Gorilla Mux can be changed by setting SkipClean to true.

    router := mux.NewRouter()
    router.SkipClean(true)
    router.HandleFunc("/new/", index)
    router.HandleFunc("/", index)
    http.ListenAndServe(":"+port, router)
    

    The relevant documentation can be found here.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
  • doushou6480 2017-04-28 01:15
    关注

    After reading the other question, I understand what do you mean. Implement a kind of URL re-writer before URL goes to gorilla/mux. The function will look like:

    func Rewriter(h http.Handler) http.Handler {
        return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
            //Simple URL rewriter. Rewrite if it's started with API path
            pathReq := r.RequestURI
            if strings.HasPrefix(pathReq, "/new/") {
                //Use url.QueryEscape for pre go1.8
                pe := url.PathEscape(strings.TrimLeft(pathReq, "/new/"))
                r.URL.Path = "/new/" + pe
                r.URL.RawQuery = ""
            }
    
            h.ServeHTTP(w, r)
        })
    }
    

    Wrap gorilla router when starting the http server:

    r := mux.NewRouter()
    
    // ... other handler
    r.HandleFunc("/new/{original-url}", NewHandler)
    
    //Wrap mux.Router using Rewriter
    log.Fatal(http.ListenAndServe(":8080", Rewriter(r)))
    

    Then in your URL shortener handler, the original URL can be extracted using the following code:

    func NewHandler(w http.ResponseWriter, r *http.Request) {
        vars := mux.Vars(r)
        ou := vars["original-url"]
        //Use url.QueryUnascape for pre go1.8
        originalURL, err := url.PathUnescape(ou)
    
        //... other processing
    }
    

    IMHO, implementing URL shortener service like this is not recommended, mainly due to incorrect use of HTTP verbs. Any GET request should not leave side effect in server e.g. no record creation in database, etc.

    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 telegram api 使用forward_messages方法转发消息时,目标群组里面会出现此消息来源,如何隐藏?
  • ¥15 在ubuntu中无法连接到远程服务器传输文件
  • ¥15 关于#tensorflow#的问题:有没有什么方法可以让机器自己学会像素风格的图片
  • ¥15 Oracle触发器字段变化时插入指定值
  • ¥15 docker无法进入容器内部
  • ¥15 qt https 依赖openssl 静态库
  • ¥15 python flask 报错
  • ¥15 改个密码引发的项目启动问题
  • ¥100 CentOS7单线多拨
  • ¥15 debian安装过程中老是出现无法将g21dr复制到g21dr怎么解决呀?