rest - POST方法中的授权使用放心
问题描述
我正在尝试向我的 API 发送一个 POST 方法,它返回 401 错误而不是 200 OK。
事情是这样的:
使用 Postman:POST 请求工作正常。
在授权部分下,我选择Basic Auth
并提供了用户名/密码。
在标题部分下,我给出了X-application
和Content-Type
。我在邮递员的身体是:
{
"description": "TestAPIPostmannolast",
"issue_type": {
"id": "271341877549072389",
"name": "Design"
},
"area": {
"id": "271341877549072406"
},
"issue_id": "9d5ac7da-9626-11e8-9eb6-529269fb1459"
}
使用 RestAssured:失败并出现 401 错误。
package basic;
import io.restassured.response.Response;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import io.restassured.RestAssured;
import static io.restassured.RestAssured.given;
public class GetRequest {
@BeforeClass
public void setup() {
RestAssured.baseURI = "https://123.com";
RestAssured.basePath = "/field-management/api";
RestAssured.authentication = RestAssured.basic("username", "password");
}
@Test
public void responseBody(){
Response response = given()
.header("X-application", "63fc4887-aed9-497f-bad5-d7ef2b90cdaf")
.contentType("application/json")
.body("{\n" +
" \"description\": \"Intellij \",\n" +
" \"issue_type\": {\n" +
" \"id\": \"271341877549072389\",\n" +
" \"name\": \"Design\"\n" +
" },\n" +
" \"area\": {\n" +
" \"id\": \"271341877549072406\"\n" +
" },\n" +
" \"issue_id\": \"2dd8ae7a-966e-11e8-9eb6-529269fb1459\n" +
"\n\"\n" +
"}")
.when()
.post("/projects/1879048400/areas/271341877549072406/issue/");
System.out.println(response.body().asString());
}
回复:
{"message":"Authentication Failed","errorKey":null}
不知道为什么当我通过 RestAssured 尝试它时它不起作用。
解决方案
添加下面的代码修复它。从邮递员那里得到这个令牌
.header("Authorization","Basic cG9sZWFyeTpBdXRoM250MWM=")
推荐阅读
- swift - SwiftUI 应用程序的 NavigationView 中的 onAppear 和 onDisappear 是否按预期运行?
- laravel - 需要有关在播放器控制器中传递 club_id 的帮助
- asp.net-core - 尝试添加剃须刀组件的向导失败
- partial-views - ASP.NET Core 在使用 @Html.EditorForModel() 时运行多个模板
- c# - 我想获取 SharePoint 列表的列表项(本地),请分享代码以使用 C# 中的属性获取列表项
- react-native - 如何在 react-native 的 createBottomTabNavigator() 中加载自定义选项卡图标
- python - Python Behave - ConfigError:[] 中没有步骤目录
- netlogo - 如何让海龟避免“着陆”补丁,同时仍朝着预定义的目标补丁移动?
- python - 访问在其外部的 WITH 块内声明的变量 - 为什么它有效?
- c# - 当调试器说它们相等时,为什么我的 assert.areequal 会失败?