首页 > 解决方案 > 如何在 Rest Assured Framework 中使用 POST 方法

问题描述

如何在放心框架中使用 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 响应正文为:{"error":"Missing email or username"}

这是参考 URL:https ://reqres.in/ 并且发布方法是:REGISTER - SUCCESSFUL

请帮我 。

谢谢

标签: apitestingqarest-assured

解决方案


您还没有添加标题,下面代码中的第三行

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

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

JSONObject requestParams = new JSONObject();

推荐阅读