Skip to content

proposal: net/http: support slash (/) in path variable #70601

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

Closed
WestleyK opened this issue Nov 27, 2024 · 5 comments
Closed

proposal: net/http: support slash (/) in path variable #70601

WestleyK opened this issue Nov 27, 2024 · 5 comments
Labels
Milestone

Comments

@WestleyK
Copy link

Proposal Details

It would be really nice if the path variables can support / in the variable, example GET /api/{resource_name}/components/info only matches /api/resource_name/components/info, and not /api/resource/name/components/info. You can url encode the slash in resource name like resource%2fname, and that does work, but not what I'm looking for.

I understand we may not want to default handle / in the path variable, but maybe an option to enable it, like {resource_name/2} for 2 slashes and {resource_name/*} for any amount.

You also cant do GET /api/{resource_name...}/components/info, which would also solve the issue I'm proposing.

Example go code
package main

import (
	"log"
	"net/http"
)

func main() {
	mux := http.NewServeMux()

	mux.Handle("GET /api/{resource_name}/components/info", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte(r.PathValue("resource_name")))
	}))

	s := http.Server{Handler: mux, Addr: ":8000"}

	log.Fatalln(s.ListenAndServe())
}
$ curl localhost:8000/api/resource_name/components/info
resource_name

$ curl localhost:8000/api/resource/name/components/info
404 page not found

$ curl localhost:8000/api/resource%2fname/components/info
resource/name
@gopherbot gopherbot added this to the Proposal milestone Nov 27, 2024
@ianlancetaylor
Copy link
Contributor

CC @jba

@ianlancetaylor ianlancetaylor moved this to Incoming in Proposals Nov 27, 2024
@jba
Copy link
Contributor

jba commented Nov 27, 2024

I think that if we allowed "..." wildcards in the middle, checking two patterns for conflict would become superlinear. I'm not positive, but that's my feeling. If there were just one wildcard, as in your example, it would be easy, but with multiple ones it seems hard.

If you want to match a fixed number of segments, you can write one pattern for each.

I think this is a likely decline.

@WestleyK
Copy link
Author

My specific use case has a lot of apis for resource_name, and resource_name allows for any number of slashes in it, I would need to make many (easily 20+) patterns, which is not maintainable. Third-party server mux (I think gorilla, chi, etc...) you can do this. But it would be really nice if you can do this with without a third-party library.

@seankhliao seankhliao changed the title proposal: net/http: Support slash (/) in path variable proposal: net/http: support slash (/) in path variable Nov 28, 2024
@jba
Copy link
Contributor

jba commented Dec 1, 2024

resource_name allows for any number of slashes in it

Typically, something like that goes at the end of the path. For example, Google Cloud Platform URLs work like that. In some cases I believe they add a :something to the end if they need one more thing; the last segment is then last_part_of_name:word, which in Go would be parsed in code.

@WestleyK
Copy link
Author

WestleyK commented Dec 1, 2024

I agree that there are better ways to implement this, I'm familiar with those api standards, unfortunately some existing apis I cannot change.

I appreciate your time and understanding in reviewing this proposal.

@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale May 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Incoming
Development

No branches or pull requests

5 participants