2

I trying to hit this api in post with java and restassured. But api has alot of queryParam with contentype- application/x-www-form-urlencoded, which cannot be send manual changing it.

Sample code is as follows-

RequestSpecification request=given();
        Response responseSample = request
                .queryParam("lastName","Sharma")
                .queryParam("firstName","Sobhit")
                .queryParam("street","523-E-BROADWAY")
                .post(url);

I have multiple parameters for sample added 3. I want read it from hashmap object and send it.

3 Answers 3

2

RestAssured API provides multiple methods to send parameters using java.util.Map. Create new map and put there parameters you need:

Map<String, String> params = new HashMap<>();
params.put("param1", "value1");
params.put("param2", "value2");

Then add this map to your request specification as:

  1. Form parameters:

    RestAssured.given()
        .formParams(params)
        .when()
        .post("http://www.example.com");
    
  2. Query parameters:

    RestAssured.given()
        .queryParams(params)
        .when()
        .post("http://www.example.com");
    
  3. Path parameters:

    RestAssured.given()
        .pathParams(params)
        .when()
        .post("http://www.example.com/{param1}/{param2}");
    

Also there's a generalized method params(parametersMap: Map): RequestSpecification, but it adds parameters as query or form params depending on request specification.

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

Comments

1

You need to change your code with:

RequestSpecification request=given();

// add the request query param
map.forEach((k, v) -> {request.queryParam(k,v);});

// send the request
Response responseSample = request.post(url);

1 Comment

I am giving a try to this but how to read multiple params keys & it values ?
1

Using rest-assured v3.0.3 we can do this:

// Put the query params in a map.
Map<String, String> queryParams = new HashMap<String, String>();
queryParams.put("lastName","Sharma");
queryParams.put("firstName","Sobhit");
queryParams.put("street","523-E-BROADWAY");

// Pass the map while creating the request object.
RequestSpecification request=RestAssured.given().queryParams(queryParams);
Response responseSample = request.post(url);

Maven dependency:

<dependency>
  <groupId>io.rest-assured</groupId>
  <artifactId>rest-assured</artifactId>
  <version>3.0.3</version>
</dependency>

4 Comments

it is a post request for which I am try with x form data. I dont think the above answer will help. @jacobcs
Are you looking to send form params? If so you can use formParamsmethods and pass the same map and do a post.
Formparams I can use but not able to create an object for it, as I have lot of keys and values which I dont want to hard code.
Where do you have it stored outside of the application? An excel/json/property file? you got to create a logic to read the outside file and convert to a map and use it later in the rest-assured call.

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.