Skip to content

Query parameters

Tambapps edited this page Mar 17, 2022 · 5 revisions

When performing a request, you can specify the params parameter to add query parameters

def posts = poet.get("/posts", params: [author: ['me', 'myself', 'I'], created_after: LocalDateTime.now().minusDays(7), 'q[something]': 'foo'])

As you can see,params is a Map with String as keys, and any kind of objects as values.

Also note that query parameters names are automatically URL encoded. You don't need to do it yourself

You can also configure default query parameters, that will be included in all your requests

poet.params['api_key'] = apiKey

Array/Collection Query parameters

There are different ways to compose arrays/Collections into query parameters. You can specify it using the enum MultivaluedQueryParamComposingType

poet.multivaluedQueryParamComposingType = MultivaluedQueryParamComposingType.REPEAT;
// same thing as above
poet.queryParamComposer.multivaluedQueryParamComposingType = MultivaluedQueryParamComposingType.REPEAT;

Here are the available values

COMMA

Will compose the list into one query parameters having all the values separated by a comma E.g. ?values=1,2,3,4

BRACKETS

Similar to COMMA but wrap the values into brackets E.g. ?values=[1,2,3,4]

REPEAT

Will compose one parameter per value in the Collection E.g. ?values=1&values=2&values=3&values=4

Query parameter composers

You can specify custom composers for custom types. By default, if no composer is found for a given type, the poet will just take it's string representation (String.valueOf(...))

E.g.

poet.queryParamComposer[Number] = { String.format('%.1f', it) }

All objects of type Number (and class extending Number like Integer, Float, etc...) will be composed using this custom composer.

You could also specify your own Date composer

poet.queryParamComposer[LocalDateTime] = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss'Z'").&format
Clone this wiki locally