![](/img/trans.png)
[英]I'm getting error 401 (unauthorized) while trying to make a GET request from my API that has a JWT token
[英]Getting Unauthorized error when trying to execute the RestSharp request that retrieves data from ArangoDB
如果我通过Postman发送http请求,那么它会起作用,并且会得到结果。 但是,当我通过RestSharp执行时,这同样行不通,并获得未经授权。
下面是代码片段:
var client = new RestClient(
"http://Username:Password@localhost:port/_db/databaseName/_api/simple/all");
var request = new RestRequest(Method.PUT);
request.AddHeader("content-type", "application/json");
request.AddParameter("application/json",
"{\n \"collection\":\"collectionName\"\n}", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
return response;
根据restsharp Wiki,您无法通过URL指定身份验证参数:
var client = new RestClient("http://example.com");
client.Authenticator = new SimpleAuthenticator("username", "foo", "password", "bar");
var request = new RestRequest("resource", Method.GET);
client.Execute(request);
通常,在这种情况下,删除https并使用wirehark或ngrep来检查网络中发生的事情是一个好主意:
GET /_db/_system/_api/version?details=true HTTP/1.1
Host: 127.0.0.1
Connection: Keep-Alive
User-Agent: ArangoDB
Accept-Encoding: deflate
Authrization: Basic cm9vdDo=
检查实际生成的身份验证标头。
var client = new RestClient("http://example.com");
client.Authenticator = new SimpleAuthenticator("admin","admin");
var request = new RestRequest(Method.GET);
client.Execute(request);
这对我有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.