1

我正在使用一个处于测试阶段的 API,它本质上是一个 Mongodb 数据库的查询字符串作为参数。Refit 正在对我的 url 进行编码,其中包含大量花括号,而他们的服务器不喜欢它,它基本上忽略了我的所有查询属性。

我尝试将整个查询作为字符串传递,这不起作用但也不会出错。我发现了一个对 Refit 的提交,它直接解决了这个问题。说要使用 header [QueryUriFormat(UriFormat.Unescaped)],这听起来不错,但我显然没有使用该QueryUriFormat属性所在的库,而且我在谷歌搜索中找不到它。

在界面中:

[QueryUriFormat(UriFormat.Unescaped)]
[Headers("Authorization: Basic")]
[Get("/stuff/etc")]
Task<myModel> GetStuff(string q);

字符串:

"filter={name:'THENAME',timestamp:{$gt:1571238110000},TOTAL_MB: {$gt:0},thingyId:{$eq:2500}}";

来电:

var result = client.GetUsageSince(stringB).Result;

我尝试了第一条评论的链接(如下)

 var HttpClient = new HttpClient(new UriQueryUnescapingHandler()) { BaseAddress = new Uri(url) };
        HttpClient.DefaultRequestHeaders.Add("Accept", "application/json");
        HttpClient.DefaultRequestHeaders.Add("Content", "application/json");
        var byteArray = Encoding.UTF8.GetBytes($"{username}:{password}");
        HttpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
        Stuff = RestService.For<IStuff>(HttpClient, refitSettings);

After doing this it looks like the absolute Uri is now correct, but the basic auth header is not working on my customer HttpClient.

4

0 回答 0