繁体   English   中英

使用 Rest Assured 参数化 Post Request 负载

[英]Parameterize Post Request payload using Rest Assured

我在 Rest Assured 代码中有以下发布请求:

我想参数化它。 请建议。

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

参数

身份证,邮箱。

当我声明字符串变量 id,email 并尝试传入 body() 时,它不起作用。

不工作的代码:

 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");

在正文中,我们需要给出确切的字符串,例如:

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

这应该有效。 但这并不是最好的方法。 您应该考虑创建一个具有 2 个字段( id 和 email )的类,并且作为请求的主体,您应该添加对象的 json 序列化主体。

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

试试这个方法。
希望能帮助到你。

除了使用POJO,您还可以使用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());

发送具有大量参数的字符串可能会变得乏味,更新具有 n 个参数的字符串可能会变得很耗时。 因此,始终建议在 body 方法中发送对象。

我建议您阅读我关于 Rest Assured 的分步教程:

使用 Rest Assured 自动化 POST 请求

看看下面的例子

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;
}

}

在上面的 Post 类中,我们创建了需要传递给 body 方法的参数的 getter 和 setter 方法。

现在我们将发送 POST 请求

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");

}

您的代码中缺少双引号。 当使用 Rest Assured 并想在 body("....") 中传递一个变量时,语法是

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

Rest Assured( "+id+" & "+email+" ) 正文中的参数应该写在双引号内。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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