【问题标题】:Parameterize Post Request payload using Rest Assured使用 Rest Assured 参数化 Post Request 有效负载
【发布时间】:2013-07-15 14:38:34
【问题描述】:

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

【问题讨论】:

  • 可能并不重要,但你的身体开头似乎缺少一个开口花括号。
  • 对不起,我错过了提供它。但我仍然遇到问题。

标签: java rest-assured


【解决方案1】:

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

"{\"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)...

试试这个方法。
希望对您有所帮助。

【讨论】:

  • 如何支持或处理嵌套参数?
【解决方案2】:

除了使用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());

【讨论】:

    【解决方案3】:

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

    我建议您阅读我关于“放心”的分步教程:

    Automating POST Request using Rest Assured

    看看下面的例子

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

    【讨论】:

      【解决方案4】:

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

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

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

      【讨论】:

      • 请说明如何解决问题。它改进或修复了什么? StackOverflow 将没有解释的帖子放在低质量帖子队列中以供审查,以查看它们是否应该被删除,您可以修复它并编辑您的答案以更具描述性。 stackoverflow.com/review/low-quality-posts
      猜你喜欢
      • 1970-01-01
      • 2016-05-10
      • 1970-01-01
      • 1970-01-01
      • 2015-07-05
      • 2021-08-23
      • 1970-01-01
      • 1970-01-01
      • 2019-06-13
      相关资源
      最近更新 更多