I have generated a simple gorilla/mux
API using swagger. For my single endpoint /v1/abc/{arg}
I am passing an argument.
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
", arg)
}
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?
Examples:
http://localhost:9090/v1/abc/hello
working as expected
http://localhost:9090/v1/abc/123/xyz
not working as expected
http://localhost:9090/v1/abc/a.x
working as expected
http://localhost:9090/v1/abc/https://google.com
not working as expected
http://localhost:9090/v1/abc/https%3A%2F%2Fgoogle.com
not working, but why?
Route setup:
var routes = Routes{
Route{
"GetAbcByArg",
strings.ToUpper("Get"),
"/v1/abc/{arg}",
GetAbcByArg,
},
}
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
It is working with
router := mux.NewRouter().SkipClean(true).UseEncodedPath()