jsnider
jsnider

Reputation: 35

Can Karate generate multiple query parameters with the same name?

I need to pass multiple query parameters with the same name in a URL, but I am having problems getting it to work with Karate. In my case, the URL should look like this:

http://mytestapi.com/v1/orders?sort=order.orderNumber&sort=order.customer.name,DESC

Notice 2 query parameters named "sort". I attempted to create these query string parameters with Karate, but only the last "sort" parameter gets created in the query string. Here are the ways I tried to do this:

Given path 'v1/orders'
    And param sort = 'order.orderNumber'
    And param sort = 'order.customer.name,DESC'
    And header Authorization = authInfo.token
    And method get
    Then status 200

And:

Given path 'v1/orders'
    And params sort = { sort: 'order.orderNumber', sort: 'order.customer.name,DESC' }
    And header Authorization = authInfo.token
    And method get
    Then status 200

And:

    Given path 'v1/order?sort=order.orderNumber&sort=order.customer.name,DESC'
    And header Authorization = authInfo.token
    And method get
    Then status 200

The first two ways provide the same query string result: ?sort=order.customer.name%2CDESC

The last example does not work because the ? get encoded, which was expected and explained in this post - Karate API Tests - Escaping '?' in the url in a feature file

It's clear that the second "sort" param is overriding the first and only one parameter is being added to the URL. I have gone through the Karate documentation, which is very good, but I have not found a way to add multiple parameters with the same name.

So, is there a way in Karate to set multiple URL query parameters with the same name?

Upvotes: 2

Views: 4387

Answers (1)

Babu Sekaran
Babu Sekaran

Reputation: 4239

Yes you can generate multiple query parameters with the same name in karate

All values of similar key should be provided in an array.

Given path 'v1/orders'
And params {"sort":["order.orderNumber","order.customer.name,DESC"]}
And header Authorization = authInfo.token
And method get
Then status 200

And for setting single parameter using param it will be like

And param sort = ["order.orderNumber","order.customer.name,DESC"]

Upvotes: 5

Related Questions