1

I'm using an API that's in beta and it's essentially a querystring for a Mongodb db as a parameter. Refit is encoding my url, which included plenty of curly braces, and their server doesn't like it and it essentially ignores all of my query attributes.

I've tried passing the entire query as a string, which doesn't work but also doesn't chuck an error. I've found a commit to Refit which directly addresses this issue here. That says to use the header [QueryUriFormat(UriFormat.Unescaped)], which sounds great, but I'm clearly not using the library the QueryUriFormat attribute is in and I can't find it in a google search.

In the interface:

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

The string:

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

The call:

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

I tried the link on the first comment (below)

 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.

Das_Geek
  • 2,775
  • 7
  • 20
  • 26
CodingCretin
  • 401
  • 4
  • 9
  • 1
    [This answer might solve your question](https://stackoverflow.com/questions/40632827/how-to-disable-urlencoding-get-params-in-refit) – So_oP Oct 18 '19 at 09:20
  • I've had a similar problem with escape characters and was looking at the same solution but it looks like the fix for QueryUriAttribute hasn't made its way into the Nuget package yet. Bummer, but this isn't ready to be used yet. You can look at the history of it here https://github.com/reactiveui/refit/commits/master/Refit/Attributes.cs – BAR Mar 06 '20 at 17:26

0 Answers0