3

I need to send the following GET request with Rest Assured:

http://some.url.com/path?filter[key1]=value1&filter[key2]=value2

I was trying to do it with queryParams and formParams, but it constructs params as filter={"key":"value"}.

In JQuery I can do this with:

$.param({filter:{key1:"value1"}})
1
  • Another option is: given(). queryParam("filter[key1]", "value1"). queryParam("filter[key2]", "value2"). when(). get("some.url.com"). then(). Commented Oct 30, 2015 at 18:00

2 Answers 2

2

According to the Rest-Assured usage guide

Parameters can also be set directly on the url:

..when().get("/name?firstName=John&lastName=Doe");

Could you not just use a formatted String with the values you want if you are not getting the desired result from the queryParam and formParam methods?

Sign up to request clarification or add additional context in comments.

4 Comments

This is an option, but not very convenient. Suppose I want to send GET request with several filters values. With parameters set directly on the url it will look like: ```
I can't see what your example is meant to be? True but you could hold all of your filters in a Map<String, String> and then build the parameters String from the map entries and concatenate it to the end of the URL?
I would like to do it something like this: given().baseUri(uri).queryParams("filter", params).when.get(/path); where params is already defined Map<String,String>: {key1:"value1", key2:"value2", key3:"value3"} But probably, I will use your option.
Sounds like you should open a pull request on github.com/jayway/rest-assured, @art_tykh.
0

I did similar thing using following way

String path = "path?";
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
            nameValuePairs.add(new BasicNameValuePair("q", search));
            nameValuePairs.add(new BasicNameValuePair("lang", lang));
            nameValuePairs.add(new BasicNameValuePair("sort", sort));
            path = path + URLEncodedUtils.format(nameValuePairs, "utf-8");
            System.out.println("REQUEST URL " + path);
            response = given().auth().preemptive().basic(user, pass).when().get(path);

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.