Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

url encoded within restapi url component #226

Open
uriklagnes opened this issue Nov 23, 2017 · 4 comments · May be fixed by #375
Open

url encoded within restapi url component #226

uriklagnes opened this issue Nov 23, 2017 · 4 comments · May be fixed by #375

Comments

@uriklagnes
Copy link

uriklagnes commented Nov 23, 2017

Hi,

I'm trying to create an API with scheme
POST /some/base/url/{{ some other url encoded here }}

Where the last part of the url is extracted by router, but it is itself an url that has been encoded by front end code.

So {{ some other url encoded here }} will be something like http://www.google.ca

(But the front end will encode this as http%3A%2F%2Fwww.google.ca first)

The problem is, when front end does the POST call, it gets a 404.
My hunch is that the router must be "seeing" the URL as the real URL and is trying to match on:
/some/base/url/http://www.google.ca <-- it is seeing the two // within url.

Is there a way for me to actually confirm this? Because the 404 is coming from the router not my app.
Is there a workaround?

Thanks

@uriklagnes uriklagnes changed the title url encoded without a url url encoded within restapi url component Nov 23, 2017
@tmthrgd
Copy link

tmthrgd commented Nov 23, 2017

path := req.URL.Path
uses req.URL.Path as the path to match against where req.URL is a url.URL. According to the documentation of url.URL:

Note that the Path field is stored in decoded form: /%47%6f%2f becomes /Go/. A consequence is that it is impossible to tell which slashes in the Path were slashes in the raw URL and which were %2f. This distinction is rarely important, but when it is, code must not use Path directly. The Parse function sets both Path and RawPath in the URL it returns, and URL's String method uses RawPath if it is a valid encoding of Path, by calling the EscapedPath method.

If you need this behaviour, try replacing that line with the following and seeing if that works:

	path := req.URL.EscapedPath() 

@Mousaka
Copy link

Mousaka commented Dec 21, 2017

@uriklagnes You might find this interesting if you are using or can use a gorilla/mux Router. There is this setting available for it:
UseEncodedPath

UseEncodedPath tells the router to match the encoded original
path to the routes.
For eg. "/path/foo%2Fbar/to" will match the path "/path/{var}/to".

If not called, the router will match the unencoded path to the
routes. For eg. "/path/foo%2Fbar/to" will match the path 
"/path/foo/bar/to"

Judging from the description it seems to solve your problem. I'm soon gonna try it out.

@pietervogelaar
Copy link

@uriklagnes You might find this interesting if you are using or can use a gorilla/mux Router. There is this setting available for it:
UseEncodedPath

UseEncodedPath tells the router to match the encoded original
path to the routes.
For eg. "/path/foo%2Fbar/to" will match the path "/path/{var}/to".

If not called, the router will match the unencoded path to the
routes. For eg. "/path/foo%2Fbar/to" will match the path 
"/path/foo/bar/to"

Judging from the description it seems to solve your problem. I'm soon gonna try it out.

I can confirm that the gorilla/mux router solves this problem. It would be nice if httprouter supports this too.

@firelizzard18
Copy link

I opened a PR to implement an option to enable this behavior

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants