JAVA中restassuredapi的request body如何传递参数?
How to pass parameter in request body of restassured api in JAVA?
In below example, if i run this it is taking z as string, not as parameter,
String z= "1234";
System.out.println("converted"+z);
try {
RestAssured.baseURI = "http://192.189.11.51:05/oceans/fssdata/";
String requestBody ="{\"CompanyID\":z"
+ ""
+ ""
+ "}";`
如何在公司ID中传递参数Z,现在是以公司ID为z,我要取z的值。
可以使用JSONObject来传递参数
import org.json.simple.JSONObject;
String z = "1234";
JSONObject requestParam = new JSONObject();
requestParams.put("CompanyID", z);
given()
.body(requestParam.toJSONString())
.post("/your_endpoint");
}
或者您可以使用 String
以您的方式传递
"{\"CompanyID\" : \""+z+"\",\n" +
"\"property\": \"value\"\n" +
"}"
直接使用字符串时需要注意转义引号。您可以尝试使用下面的代码。
String z = "\"1234\"";
String requestBody: "{\"name\":"+z+"}";
你可以使用这个:
行家
com.github.cliftonlabs
import com.github.cliftonlabs.json_simple.JsonObject;
public void createAnAddress() {
RestAssured.baseURI = "http://192.189.11.51:05/oceans/fssdata/";
JsonObject requestParams = new JsonObject();
requestParams.put("CompanyID", "Monroe Company");
Response response = given().
.header("Content-Type", "application/json").body(requestParams)
.post("/oceans/fssdata/");
System.out.println("Response Body create Add is => " + response.getBody().asString());
}
In below example, if i run this it is taking z as string, not as parameter,
String z= "1234";
System.out.println("converted"+z);
try {
RestAssured.baseURI = "http://192.189.11.51:05/oceans/fssdata/";
String requestBody ="{\"CompanyID\":z"
+ ""
+ ""
+ "}";`
如何在公司ID中传递参数Z,现在是以公司ID为z,我要取z的值。
可以使用JSONObject来传递参数
import org.json.simple.JSONObject;
String z = "1234";
JSONObject requestParam = new JSONObject();
requestParams.put("CompanyID", z);
given()
.body(requestParam.toJSONString())
.post("/your_endpoint");
}
或者您可以使用 String
以您的方式传递"{\"CompanyID\" : \""+z+"\",\n" +
"\"property\": \"value\"\n" +
"}"
直接使用字符串时需要注意转义引号。您可以尝试使用下面的代码。
String z = "\"1234\"";
String requestBody: "{\"name\":"+z+"}";
你可以使用这个:
行家 com.github.cliftonlabs
import com.github.cliftonlabs.json_simple.JsonObject; public void createAnAddress() { RestAssured.baseURI = "http://192.189.11.51:05/oceans/fssdata/"; JsonObject requestParams = new JsonObject(); requestParams.put("CompanyID", "Monroe Company"); Response response = given(). .header("Content-Type", "application/json").body(requestParams) .post("/oceans/fssdata/"); System.out.println("Response Body create Add is => " + response.getBody().asString()); }