How to handle query parameters using ratpack handlers?

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

How to handle query parameters using ratpack handlers?

tanmayghosh2507
I mean I have to build an API like this: GET host:5050://cars?brand={brand}&cost={cost}
handlers {
   ServerProperties serverProperties ->
       prefix(cars) {
           get('/?:info_param=:info&cost=:cost') {
           //Consumer can either query based on brand and cost of a car OR type and cost of a car. I don't want to build 2 separate APIs for the
           //same by adding one more field before the query parameters. I want to recieve this request as a query param and then redirect accordingly.
           def info_param = pathTokens.get('info_param')
           if(info_param = 'brand') { //Sample API call: /cars?brand=bmw&cost=5000
               registry.get(CarsBrandGetEndpoint).handle(context)
           }
           else if(info_param = 'type') { //Sample API call: /cars?type=luxury&cost=10000
               registry.get(CarsCostGetEndpoint).handle(context)
           }
       }
   }
}
Can I ?
Reply | Threaded
Open this post in threaded view
|

Re: How to handle query parameters using ratpack handlers?

danveloper
Administrator
was Luke's response in Slack yesterday not enough?

Reply | Threaded
Open this post in threaded view
|

Re: How to handle query parameters using ratpack handlers?

danhyun
In reply to this post by tanmayghosh2507
Hey there,

We don't bind on query params, we bind on path. To get to query params you can access via Request object

https://ratpack.io/manual/current/api/ratpack/http/Request.html#getQueryParams--

You can get request via context

get {

  def params = request.queryParams

  // do stuff with params

  render 'ok'
}
Reply | Threaded
Open this post in threaded view
|

Re: How to handle query parameters using ratpack handlers?

tanmayghosh2507
Yes, I tried to do the same, but still not working. I am not getting the parameters by doing request.queryParams or request.getQueryParams(), as I tried to print the same.
If possible can you please share a git repo link where this query parameters in implemented already.
Thanks
Reply | Threaded
Open this post in threaded view
|

Re: How to handle query parameters using ratpack handlers?

tanmayghosh2507
Will the query parameters will also be passed as context? or I have to parse them differently. Currently I am parsing as def var = Context.allPathTokens("field")
Reply | Threaded
Open this post in threaded view
|

Re: How to handle query parameters using ratpack handlers?

davidmc24
In reply to this post by tanmayghosh2507
[example-books](https://github.com/ratpack/example-books/blob/master/src/ratpack/ratpack.groovy#L77) uses query parameters.

While in this case it's used for internal purposes (passing status messages to subsequent pages), it demonstrates the technique.

If you run the app with `./gradlew run` or `gradlew.bat run` (depending on platform) and go to http://localhost:5050/?msg=LookAtMeThisIsMyQueryParam!!! you should see a message at the top of the page.