Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在JAVA中如何在重启api的请求体中传递参数?_Java_Api_Selenium_Rest Assured_Rest Assured Jsonpath - Fatal编程技术网

在JAVA中如何在重启api的请求体中传递参数?

在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

如何在公司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 {
            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+"}";
    

先生,我尝试了您在第二点字符串requestBody=“{“\”CompanyID\:\“z\”}++“+”}”中提到的方法;如果您想在JSON字符串中使用多行,请将错误获取为无效赋值运算符或编辑第二部分的答案
 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());
 }