Dart基本认证不工作(dart:io)(更新为工作代码) [英] Dart basic auth not working (dart:io) (UPDATED to working code)
问题描述
我正在使用Harvest API,一个非常标准的网络服务API,我的curl请求正常工作,而我的Dart HttpClient请求不是。这里是我的curl请求(当然是伪装的敏感信息):
I'm working with the Harvest API, a pretty standard web service API, and my curl requests are working just fine while my Dart HttpClient requests are not. Here is my curl request (with sensitive information disguised, of course):
curl -H "Content-Type: application/json" \
-H "Accept: application/json" \
-u "address@domain.com:password" \
https://my_domain.harvestapp.com/account/who_am_i
UPDATE ---以下代码现在可以正常工作: p>
UPDATE --- The following code now works:
HttpClient client = new HttpClient();
client.addCredentials(
Uri.parse('https://my_domain.harvestapp.com/account/who_am_i'),
'realm',
new HttpClientBasicCredentials('address@domain.com', 'password')
);
client.getUrl(Uri.parse('https://my_domain.harvestapp.com/account/who_am_i'))
.then((HttpClientRequest req) {
req.headers
..add(HttpHeaders.ACCEPT, 'application/json')
..add(HttpHeaders.CONTENT_TYPE, 'application/json');
return req.close();
})
.then((HttpClientResponse res) {
print(res);
client.close();
});
}
显然,我想做的不仅仅是简单的 print
响应,但无论如何, res
对象最终为 null
意味着请求在某些方面失败。有没有什么似乎是错误或失败?
Obviously I would like to do more than simply print
the response, but no matter what, the res
object ends up being null
, which means that the request is failing in some respect. Does anything seem awry or amiss? I'm at a loss for now.
推荐答案
总结原始代码段的变化:
To summarize the changes from the original code snippet:
- 在调用
之前调用
c> c $>HttpClient.addCredentials
HttpClient.getUrl
-
- 在
.then((HttpClientRequest))
) - 确保
addCredentials
和getUrl
匹配。
- Call
HttpClient.addCredentials
before callingHttpClient.getUrl
- In the
.then((HttpClientRequest))
part, make sure toreturn req.close()
- Make sure the URLs in
addCredentials
andgetUrl
match.
这篇关于Dart基本认证不工作(dart:io)(更新为工作代码)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!