java 使用 Rest Assured 参数化 Post Request 负载

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17656881/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 14:34:30  来源:igfitidea点击:

Parameterize Post Request payload using Rest Assured

javarest-assured

提问by dileepvarma

I have below post request in Rest Assured code :

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

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.

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

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

回答by mihai.ciorobea

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.

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

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

Try this way.
Hope it helps.

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

回答by Johan

Besides using a POJOyou can also use a HashMap:

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

回答by Ashwiinn Karaangutkar

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.

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

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

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

Automating POST Request using Rest Assured

使用 Rest Assured 自动化 POST 请求

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.

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

Now we will send the POST request

现在我们将发送 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");

}

回答by Taz

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

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

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

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

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

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