以下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请求
答案 0 :(得分:0)
如果您正在使用Rest Assured进行API测试。您必须将contentType指定为“ application / json”,在post方法中,您需要传递端点。