使用“确保放心”的HTTP POST请求(示例)

时间:2019-06-27 15:59:15

标签: 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 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
}

在“确保放心-Java”中可以将此代码用于POST请求

1 个答案:

答案 0 :(得分:0)

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