2

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?

1 Answer 1

5

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"]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.