在JAVA中如何在重启api的请求体中传递参数?
如何在公司ID中传递参数Z,现在它将公司ID作为Z,我希望它应该取Z的值。您可以使用JSONObject传递参数在JAVA中如何在重启api的请求体中传递参数?,java,api,selenium,rest-assured,rest-assured-jsonpath,Java,Api,Selenium,Rest Assured,Rest Assured Jsonpath,如何在公司ID中传递参数Z,现在它将公司ID作为Z,我希望它应该取Z的值。您可以使用JSONObject传递参数 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 { RestAs
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"
+ ""
+ ""
+ "}";`
或者,您可以使用字符串按自己的方式传递
import org.json.simple.JSONObject;
String z = "1234";
JSONObject requestParam = new JSONObject();
requestParams.put("CompanyID", z);
given()
.body(requestParam.toJSONString())
.post("/your_endpoint");
}
直接使用字符串时,需要小心转义引号。您可以尝试使用下面的代码
"{\"CompanyID\" : \""+z+"\",\n" +
"\"property\": \"value\"\n" +
"}"
您可以使用以下选项:
- 马文
com.github.cliftonlabs
String z = "\"1234\""; String requestBody: "{\"name\":"+z+"}";
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());
}