Skip to content

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

Closed as not planned
Closed as not planned
@WestleyK

Description

@WestleyK

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    Status

    Incoming

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions