首页 > 解决方案 > 无法使用 REST Assured 生成 Auth 令牌

问题描述

我正在尝试为我在本地机器上托管的 API 生成访问令牌。我可以使用 Postman 检索访问令牌。但我无法使用 RestAssured 做同样的事情。

下面是我为此编写的代码片段。

Response authentication = RestAssured.given().auth()
.basic("admin", "password123").header("content-type", "application/json")
.when()
.post("http://localhost:3001/auth/");

回复 :

 {"reason":"Bad credentials}

如果我遗漏了任何内容,请帮助我弄清楚这一点。

标签: rest-assured

解决方案


 Response authentication = RestAssured.given().auth().preemptive()
 .basic("admin","password123")
 .header("content-type", "application/json")
 .when().post("http://localhost:3001/auth");

https://github.com/rest-assured/rest-assured/wiki/Usage#basic-authentication


推荐阅读