将URL作为URL参数传递

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

Passing a URL as URL param

问题

我有一个使用路由参数编写的路由:

/properties/configurations/:name/:value

当我使用一个URL作为value进行调用时,像这样:

/properties/configurations/app.url/http://test.test

我猜测它没有命中我的Revel控制器,可能是因为斜杠引起了混淆。我尝试使用encodeURIComponent()来调用JavaScript请求,以便删除斜杠和其他有问题的字符,但它仍然没有命中控制器,并返回404错误。

问题是它在一个公共API中使用,我不能将其更改为在请求体中传递这些数据,所以我正在努力找出如何让Revel识别这个模式,并正确地将值放入{name}和{value}中。

英文:

I have this route that was written with route params

/properties/configurations/:name/:value

and when I call with with a URL as value like that

/properties/configurations/app.url/http://test.test

it doesn't hit my Revel controller I guess because the slash confuses it. I tried when calling the Javascript request with encodeURIComponent() so it removes the slashes and other chars that are problematic but it still doesn't hit the controller and gives 404.

The problem is it is used in a public API and I cannot change it to pass this data in the body instead so I'm trying to figure out how can I make revel recognize the pattern and correctly put the values inside {name} and {value}

答案1

得分: 1

你只需要使用其中一个转义函数:

package main
import "net/url"

func main() {
   s := "http://test.test"
   {
      t := url.PathEscape(s)
      println(t == "http:%2F%2Ftest.test")
   }
   {
      t := url.QueryEscape(s)
      println(t == "http%3A%2F%2Ftest.test")
   }
}
英文:

You just need to use one of the escape functions:

package main
import "net/url"

func main() {
   s := "http://test.test"
   {
      t := url.PathEscape(s)
      println(t == "http:%2F%2Ftest.test")
   }
   {
      t := url.QueryEscape(s)
      println(t == "http%3A%2F%2Ftest.test")
   }
}

答案2

得分: 0

如果你能将URL编码的URL作为路径变量传递给你的Golang服务器,你可以通过url.PathUnescape(str)来处理它。

如果问题是你的路由器无法正确到达相应的处理程序,可能是因为路径变量中的字符没有正确匹配,这可能是你错误定义了路径匹配器。我对revel没有经验,但是对于mux(github.com/gorilla/mux),我期望它的定义如下:

package main

import (
    "net/http"
    "net/url"
    "github.com/gorilla/mux"
)

func main() {

    r := mux.NewRouter()
    r.HandleFunc("/properties/configurations/app.url/{nestedURL:.*}", myHandler)
 
    http.ListenAndServe(":8080", r)

}

func myHandler(w http.ResponseWriter, r *http.Request){
    nestedURL := mux.Vars(r)["nestedURL"]
    unescapedPath, err := url.PathUnescape(nestedURL)
    if err != nil {
        // 处理错误
    }
}

上面的示例甚至可能在不需要对嵌套URL进行URL编码的情况下工作(但我真的建议你对其进行URL编码)。

英文:

If you're able to pass the URL encoded URL to your Golang server as a path variable, you can run it through url.PathUnescape(str).

If the problem is that your router isn't reaching the correct handlers because the characters in your path variable aren't matching correctly, it's possible that you've incorrectly defined that path matcher. I don't have experience with revel, but with mux (github.com/gorilla/mux) I would expect it to look like the following:

package main

import (
    "net/http"
    "net/url"
    "github.com/gorilla/mux"
)

func main() {

    r := mux.NewRouter()
    r.HandleFunc("/properties/configurations/app.url/{nestedURL:.*}", myHandler)
 
    http.ListenAndServe(":8080", r)

}

func myHandler(w http.ResponseWriter, r *http.Request){
    nestedURL := mux.Vars(r)["nestedURL"]
    unescapedPath, err := url.PathUnescape(nestedURL)
    if err != nil {
        // handle err
    }
}

The above example might even work without requiring you to URL encode the nested URL (but I really do suggest you URL encode it).

huangapple
  • 本文由 发表于 2021年6月9日 22:10:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/67905860.html
匿名

发表评论

匿名网友

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

确定