简体   繁体   English

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

[英]HTTP POST Request using Rest Assured (Example)

Below POST Method can be used to run the HTTP POST request in Rest Assured using Java 下面的POST方法可用于在Rest Restured中使用Java运行HTTP POST请求

better to go with GSON dependency 与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
}

Can use this code for POST Request in Rest Assured - Java 可以在Rest Assured中将此代码用于POST请求-Java

If you are doing API testing using Rest Assured. 如果您正在使用Rest Assured进行API测试。 You have to specify the contentType to "application/json" and in post method you need to pass the endpoint. 您必须将contentType指定为“ application / json”,在post方法中,您需要传递端点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM