Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/401.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 使用Rest-Assured的HTTP POST请求(示例)_Java_Rest_Post_Rest Assured_Rest Assured Jsonpath - Fatal编程技术网

Java 使用Rest-Assured的HTTP POST请求(示例)

Java 使用Rest-Assured的HTTP POST请求(示例),java,rest,post,rest-assured,rest-assured-jsonpath,Java,Rest,Post,Rest Assured,Rest Assured Jsonpath,下面的POST方法可用于使用Java在Rest Assured中运行HTTP POST请求 最好使用GSON依赖 { RestAssured.baseURI = API_URL; RequestSpecification request = RestAssured.given(); request.header("Key1", "Value1"); request.header("Key2", ""+Value2+""); //If value is gett

下面的POST方法可用于使用Java在Rest Assured中运行HTTP POST请求

最好使用GSON依赖

{
    RestAssured.baseURI = API_URL;

    RequestSpecification request = RestAssured.given();

    request.header("Key1", "Value1");
    request.header("Key2", ""+Value2+""); //If value is getting capture from other variable

    JSONObject requestParams = new JSONObject();
    requestParams.put("Payload Key1", "Payload Value1"); 
    requestParams.put("Payload Key2", "Payload Value2");

    request.body(requestParams.toString());
    Response response = request.post(""); //last word of URL
    int StatusCode = response.getStatusCode(); //Get Status Code
    System.out.println("Status code : " + StatusCode);       
    System.out.println("Response body: " + response.body().asString()); //Get Response Body
}

如果您使用Rest-Assured进行API测试,则可以在Rest-Assured-Java中使用此代码进行POST请求。您必须将contentType指定为“application/json”,并在post方法中传递端点

如果您使用Rest-Assured进行API测试。您必须将contentType指定为“application/json”,并在post方法中传递端点

那么你的问题是什么?:)那么你的问题是什么?:)