如何在Rest Assured Framework中使用POST方法

时间:2018-10-29 10:57:17

标签: api testing qa rest-assured

如何在放心的框架中使用post方法。 我正在使用Rest Assured Framework学习API,有人可以帮助我如何构造

我的代码:

    RestAssured.baseURI ="https://reqres.in";
    RequestSpecification request = RestAssured.given();

    JSONObject requestParams = new JSONObject();        
    requestParams.put("email",  "sydney@fife");
    requestParams.put("password", "pistol444");
    request.body(requestParams.toJSONString());
    Response response = request.post("/api/register");

    int statusCode = response.getStatusCode();
    System.out.println("API response code is :" + statusCode);
    String body =response.asString();
    System.out.println("Response body is : "+ body);

使用此Post方法后获取错误消息:

API响应代码为:400 响应正文为:{“错误”:“电子邮件或用户名丢失”}

这是参考URL:https://reqres.in/ 张贴方法是:注册-成功

请帮助我。

谢谢

1 个答案:

答案 0 :(得分:2)

您尚未在下面的代码中添加标题(第三行)

RestAssured.baseURI = "https://reqres.in";

RequestSpecification request = RestAssured.given();
request.header("Content-Type","application/json");

JSONObject requestParams = new JSONObject();