网址转义参数无法正确解析

时间:2019-04-16 21:01:32

标签: go swagger mux

我已经使用swagger生成了一个简单的gorilla/mux API。 对于我的单个端点/v1/abc/{arg},我正在传递一个参数。

因此,诸如http://localhost:9090/v1/abc/hello之类的请求现在仅回显该参数。 以下函数处理请求:

func GetAbcByArg(w http.ResponseWriter, r *http.Request) {
    w.Header().Set("Content-Type", "application/json; charset=UTF-8")
    vars := mux.Vars(r)
    arg, _ := url.QueryUnescape(vars["arg"])
    log.Printf("arg %s", arg)
    w.WriteHeader(http.StatusOK)
    fmt.Fprintf(w, "%v\n", arg)
}

到目前为止,一切都很好。但是,每当我尝试传递一些特殊字符作为参数时,都会出现404错误,并且不会调用处理程序。我虽然可以通过完全转义URL字符(rfc3986)来简单地转义该参数,但这也不起作用。为什么?如何准备任何字符串,以便可以在单个元素中传递它?

示例:

http://localhost:9090/v1/abc/hello正常工作

http://localhost:9090/v1/abc/123/xyz不能按预期工作

http://localhost:9090/v1/abc/a.x正常工作

http://localhost:9090/v1/abc/https://google.com不能按预期工作

http://localhost:9090/v1/abc/https%3A%2F%2Fgoogle.com无法正常工作,但是为什么

更新

路由设置:

var routes = Routes{
    Route{
        "GetAbcByArg",
        strings.ToUpper("Get"),
        "/v1/abc/{arg}",
        GetAbcByArg,
    },
}

2 个答案:

答案 0 :(得分:0)

Replacing one of the / in this pages address is still pointing to this page:

https://stackoverflow.com/questions/55716545%2Furl-escaped-parameter-not-resolving-properly

So I guess

http://localhost:9090/v1/abc/https%3A%2F%2Fgoogle.com

Is seen as http://localhost:9090/v1/abc/https%3A//google.com

By your router and it doesn't seem to allow any more than one part after abc

答案 1 :(得分:0)

正在使用

router := mux.NewRouter().SkipClean(true).UseEncodedPath()

ref:https://github.com/gorilla/mux/blob/master/mux.go

相关问题