Chi: Proper way to query URL params?

Created on 1 May 2017  路  2Comments  路  Source: go-chi/chi

I realize this probably belongs at StackOverflow, but I wonder if there is a cool 'chi' solution rather than a 'Go' solution, so I hope you don't mind me asking here..

Imagine you're building an image serving backend, where you can specify different parameters, such as:

example.com/Jd8saD.jpg?width=420&height=320&fit=crop&rotate=90

Each of these parameters (which could be included in any order) calls different functions to process the image. What would be the best way to go about doing this? Maybe use a single GET function like this:

function ImageGET(w http.ResponseWriter, r *http.Request) {
  if r.URL.Query().Get("width") != "" {
    // do something
  }
  if r.URL.Query().Get("height") != "" {
    // do something
  }
  if r.URL.Query().Get("crop") != "" {
    // do something
  }
  etc
}

?

By the way, is it even possible to check for this route? I know I could do:

r.Get("/:image", ImageGET)

but if I do that, then I assume there might be some conflicts with the other routes?

Most helpful comment

All 2 comments

For those looking for the same thing few years later, you can use if chi.URLParam(r, "width") != "" ...

Was this page helpful?
0 / 5 - 0 ratings

Related issues

rickb777 picture rickb777  路  12Comments

jsadwith picture jsadwith  路  6Comments

mvrlin picture mvrlin  路  6Comments

valsor picture valsor  路  5Comments

nhooyr picture nhooyr  路  12Comments