POST请求放心

时间:2015-08-07 10:28:12

标签: rest rest-assured

我正在使用包含JSON正文的帖子请求

我的帖子请求代码是: -

RestAssuredResponseImpl stat=
            (RestAssuredResponseImpl)given().
            header("Accept", "application/json").
            header("Content-Type", "application/json").
            header("userid", "131987”).
            queryParam("name", "Test12").
            queryParam("title", "Test127123").
            queryParam("contactEmail", “abc@gmail.com").
            queryParam("description", "testing purpose").
            when().post("").thenReturn().getBody();

我收到以下错误: -

{"errors":{"error":{"code":400,"type":"HttpMessageNotReadableException","message":"Required request body content is missing: org.springframework.web.method.HandlerMethod$HandlerMethodParameter@8e9299c"}}}

请帮助....

3 个答案:

答案 0 :(得分:0)

检查POST URI的完全限定路径。缺少电话会议的正文,这是强制性的。 你的电话应该是:

Response _res = requestspec.body(jsonObject).post(url);

然后您可以对Response执行操作。

答案 1 :(得分:0)

//尝试使用此代码,它可以帮助我也为您提供帮助。

    {
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(""); 
int StatusCode = response.getStatusCode(); 
System.out.println("Status code : " + StatusCode);       
System.out.println("Response body: " + response.body().asString()); //Get Response Body
}

答案 2 :(得分:0)

当必须在URL中传递查询参数时使用

queryParam         部分,我们必须发送实际内容。

您可以通过以下方式发送内容:

### 1.Passing JSON data format in the body:-
    given() 
    .accept(ContentType.JSON) 
    .contentType(ContentType.JSON) 
    .body("{\r\n" + "    \"id\": 101,\r\n" +
    "    \"firstName\": \"john\",\r\n" + "    \"lastName\": \"doe\",\r\n" +
    "    \"email\": \"loahsda@gmail.com\",\r\n" +
    "    \"mobile\": \"981-232-3435\",\r\n" + "    \"dateOfBirth\": 78468436\r\n"
    + "}" )
    .post(URL)
    .then() 
    .statusCode(200).log().all() 

### Second Way
    JSONObject json1 = new JSONObject(); 
    json1.put("firstName","John");
    json1.put("lastName","Joker"); 
    json1.put("email","loahsda@gmail.com");
    json1.put("mobile","981-232-3435"); 
    json1.put("dateOfBirth",78468436);
    Response response1 = 
    given()
    .pathParam("name", "object1")
    .accept(ContentType.JSON) 
    .contentType(ContentType.JSON)
    .body(json1.toString())
    .post("https://xxx/{name}") 
    .then()
    .statusCode(200)
    .log().all();
相关问题