1

I'm using labstack Echo (v4.1.11) go framework to create a REST API.

How do we/can we pass map query param in the URL? I tried adding the following struct for the request body

PaginationRequest struct {
    Page   int64             `query:"page"`
    Limit  int64             `query:"limit"`
    Filter map[string]string `query:"filter"`
}

and hoping I could got this url http://host/endpoint?page=1&limit=10&filter[status]=1&filter[user_id]=12 to work (read the filter["status"] and filter["user_id"]

But when I tried binding the incoming request to PaginationRequest it only read the page and limit parameter and filter = nil

I tried debug it into the echo/bind.go the Bind function in reading the QueryParam actually read filter[status] as plain string "filter[status]" as key.

Is there anyway we can pass the map to the url query param? or we really have to be specific in here?

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Andy
  • 449
  • 5
  • 13
  • 1
    *"Is there anyway we can pass the map to the url query param?"* no, there isn't. *"or we really have to be specific in here?"* only if implementing your own solution, or looking for a 3rd party solution that does what you want, is something you wanna avoid then yes, you have to be specific. – mkopriva Oct 30 '19 at 13:14
  • I can understand if it makes much complication to the code it's better be specific, but why is it something we want to avoid? if we can add one single dynamic filter for the client to request, wouldn't it be simpler? client can filter whatever field they want by doing so. – Andy Oct 30 '19 at 21:52
  • I did not say that you want to avoid XYZ. What I said was that, **only if** you want to avoid XYZ, **then** the only option left is ABC. The comment does not state which one is worse nor does it state which one is better, and it also does not state which one should be avoided nor does it state which one should be preferred. – mkopriva Oct 30 '19 at 21:58
  • ah my bad, just re-read it through again. thanks. – Andy Oct 30 '19 at 22:17

0 Answers0