带有保证请求的问题的问题“ valo_error.missing&quot”

发布于 2025-01-25 07:57:30 字数 1131 浏览 1 评论 0原文

我正在尝试通过REST保证的请求传递请求主体参数,但是它返回我非常令人沮丧,我正在尝试使用JSON格式(例如JSON格式)尝试不同的方式,但工作也始终同样的错误。 错误消息 value_error.missing 对于所有身体参数。

   value_error.missing
{
  "detail": [
    {
      "loc": [
        "body",
        "username"
      ],
      "msg": "field required",
      "type": "value_error.missing"
    },
    {
      "loc": [
        "body",
        "password"
      ],
      "msg": "field required",
      "type": "value_error.missing"
    }
  ]
}

测试代码:

public Response login(String username, String password, String proxy) {
    //String user = "{\"username\":\"" + username + "\",\"password\":\"" + password + "\",\"proxy\":\"" + proxy + "\"}";
    Map<String, String> user = new HashMap<>();
    user.put("username", username);
    user.put("password", password);
    user.put("proxy", proxy);

    Response response = given()
            .headers(httpHeaderManager())
            .body(user)
            .spec(urlUser)
            .post("/auth/login")
            .then()
            .extract().response()
    
    
    return response;
    
}

I am trying to pass request body parameters in rest assured request, but it returns me with is very frustrating I am trying different ways I try with String like JSON format but is not working also always same error.
Error message value_error.missing for all body parameters.

   value_error.missing
{
  "detail": [
    {
      "loc": [
        "body",
        "username"
      ],
      "msg": "field required",
      "type": "value_error.missing"
    },
    {
      "loc": [
        "body",
        "password"
      ],
      "msg": "field required",
      "type": "value_error.missing"
    }
  ]
}

The test code:

public Response login(String username, String password, String proxy) {
    //String user = "{\"username\":\"" + username + "\",\"password\":\"" + password + "\",\"proxy\":\"" + proxy + "\"}";
    Map<String, String> user = new HashMap<>();
    user.put("username", username);
    user.put("password", password);
    user.put("proxy", proxy);

    Response response = given()
            .headers(httpHeaderManager())
            .body(user)
            .spec(urlUser)
            .post("/auth/login")
            .then()
            .extract().response()
    
    
    return response;
    
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

芯好空 2025-02-01 07:57:30
JSONObject jsonObj = new JSONObject();
jsonObj.put("username", username);
jsonObj.put("password", password);
jsonObj.put("proxy", proxy);

RequestBody body = RequestBody.create(jsonObj.toString(), MediaType.parse("application/json; charset=utf-8"));

Response response = given()
        .headers(httpHeaderManager())
        .body(body)
        .spec(urlUser)
        .post("/auth/login")
        .then()
        .extract().response()

这应该起作用

JSONObject jsonObj = new JSONObject();
jsonObj.put("username", username);
jsonObj.put("password", password);
jsonObj.put("proxy", proxy);

RequestBody body = RequestBody.create(jsonObj.toString(), MediaType.parse("application/json; charset=utf-8"));

Response response = given()
        .headers(httpHeaderManager())
        .body(body)
        .spec(urlUser)
        .post("/auth/login")
        .then()
        .extract().response()

This should work

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文