Url 转义参数未正确解析 [英] Url escaped parameter not resolving properly

查看:41
本文介绍了Url 转义参数未正确解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have generated a simple gorilla/mux API using swagger. For my single endpoint /v1/abc/{arg} I am passing an argument.

因此像这样的请求:http://localhost:9090/v1/abc/hello 只是暂时回显参数.以下函数处理请求:

Therefore a request like: http://localhost:9090/v1/abc/hello simply echos the argument for now. The following function handles the request:

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) 来简单地转义参数,但这也不起作用.为什么?我如何准备任何字符串,以便我可以在单个元素中传递它?

So far so good. However, whenever I try to pass some special characters as the argument, I get a 404 error and the handler is not called. I though I could simply escape the argument by fully escaping URL characters (rfc3986), but that is also not working. Why? How can I prepare any string so I can pass it in a single element?

示例:

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,
    },
}

推荐答案

It is working with

It is working with

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

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

这篇关于Url 转义参数未正确解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆