Reputation: 518
I have below post request in Rest Assured code :
I want to parameterize it. Please suggest.
given().contentType(JSON).with()
.body("\"id\":"123",\"email\":\"[email protected]\"}").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 protected];
given().contentType(JSON).with()
.body("\"id\":id,\"email\":email}").expect().body("status",
notNullValue()).when().post("https://localhost:8080/product/create.json");
Upvotes: 1
Views: 20567
Reputation: 113
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.
Upvotes: 0
Reputation: 171
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");
}
Upvotes: 0
Reputation: 40628
Besides using a POJO 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());
Upvotes: 0
Reputation: 741
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.
Upvotes: 3