julienschmidt / httprouter

A high performance HTTP request router that scales well
https://pkg.go.dev/github.com/julienschmidt/httprouter
BSD 3-Clause "New" or "Revised" License
16.57k stars 1.47k forks source link

How can I receive multiple parameters in GET request? #338

Open k0fi opened 3 years ago

k0fi commented 3 years ago

I'd like to handle an HTTP GET request, like this:

/search/projects?page=0&min=0&max=10000&ids=57,28,55,53,51,611,619,610,578,96,4,177

As far as I see in the docs only one parameter is mentioned in get requests. So I'm wondering how can I achieve a multi-parameter GET?

dbubel commented 2 years ago

You should be able to get the query params like this off your request r.

min := r.URL.Query().Get("min")
max := r.URL.Query().Get("max")

I think what you are asking about is httprouter.Params. Say you have this route /api/users/:userId you could access userId in your handler as shown below.

func users(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
 userId: = p.ByName("userid")
}
go-aegian commented 2 years ago

I made some changes to the project but waiting on approval to get them merged, in the meantime what you are asking can be done check it out here https://github.com/sainzg/httprouter/blob/main/params_test.go - TestMissingContextParams

letmestudy commented 2 years ago

这是来自QQ邮箱的假期自动回复邮件。   您好,我最近正在休假中,无法亲自回复您的邮件。我将在假期结束后,尽快给您回复。