简体   繁体   中英

Parameterize Post Request payload using Rest Assured

I have below post request in Rest Assured code :

I want to parameterize it. Please suggest.

       given().contentType(JSON).with()
          .body("\"id\":"123",\"email\":\"abv@gmail.com\"}").expect().body("status",            
        notNullValue()).when().post("https://localhost:8080/product/create.json");

Parameters

id, email.

When I declared String variables id,email and try passing in body() its not working.

Not working code:

 String id="123";
 String email=abc@gmail.com;

 given().contentType(JSON).with()
  .body("\"id\":id,\"email\":email}").expect().body("status",              
   notNullValue()).when().post("https://localhost:8080/product/create.json");

In body we need to give the exact string like :

"{\"id\":" + id + ",\"email\":" + email + "}"

This should work. But this is not the best approach. You should consider creating a class with 2 fields( id and email ), and as body to the request you should add the json-serialised body of the object.

LoginRequest loginRequest = new LoginRequest(id, email);
String loginAsString = Util.toJson(loginRequest);
given().contentType(JSON).with()
   .body(loginAsString)...

Try this way.
Hope it helps.

Besides using aPOJO you can also use a HashMap :

given().
        contentType(JSON).
        body(new HashMap<String, Object>() {{
            put("name", "John Doe");
            put("address", new HashMap<String, Object>() {{
                put("street", "Some street");
                put("areaCode", 21223);
            }});
        }}).
when().
        post("https://localhost:8080/product/create.json")
then().
        body("status",  notNullValue());

Sending a string with a large number of the parameter can become tedious and updating a string having n number of parameters may become time-consuming. Hence, it is always recommended to send an object in body method.

I would advise you to go through my step by step tutorial on Rest Assured:

Automating POST Request using Rest Assured

Have a look at below example

public class Posts {

public String id;
public String title;
public String author;

public void setId (String id) {

this.id = id;
}

public void setTitle (String title) {

this.title = title;
}

public void setAuthor (String author) {

this.author = author;

}

public String getId () {

return id;

}

public String getTitle () {

return title;
}

public String getAuthor () {

return author;
}

}

In the above Post class, we have created getter and setter methods of the parameters that we need to pass to the body method.

Now we will send the POST request

import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import static com.jayway.restassured.RestAssured.*
import com.jayway.restassured.RestAssured;
import com.jayway.restassured.http.ContentType;
import com.jayway.restassured.response.Response;
import com.restapiclass.Posts;

public class PostRequestTest {


@BeforeClass
public void setBaseUri () {

RestAssured.baseURI = "http://localhost:3000";
}


@Test
public void sendPostObject () {

Posts post = new Posts();
post.setId ("3");
post.setTitle ("Hello India");
post.setAuthor ("StaffWriter");

given().body (post)
.when ()
.contentType (ContentType.JSON)
.post ("/posts");

}

You are missing double quotes in your code. When using Rest Assured and want to pass a variable inside the body("...."), the syntax is

"{\\"id\\": \\""+id+"\\", \\"email\\": \\""+email+"\\"}";

parameters inside the body of Rest Assured( "+id+" & "+email+" ) should be written inside double-quotes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM