5

There is a way to use Refit to input parameters in a dynamic way?

I have this code in my Refit`s Interface:

      [Get("/click?{parm}")]
      Task<ApiResponse<TAny>> SaveClick(string parm);

the value of parm is:

        "id=1234&x=567"

my route:

        [HttpGet]
        [Route("click")]
        public  void test ([FromQuery] string id)
        {
            Ok("Ok");
        }

All Im getting is that the value from id parameter is null. The expected result would be a string with value "1234"

Any help :D?

  • I don't know refit, but wouldn't the following help `[Route("click/{id}")]`? – Isolin Jan 15 '22 at 22:06
  • Unfortunately I dont have access to the route that is used to send the data, the thing is that I dont know what parameters are being expecting for the route... thats why Im passing them as string ("id=1234&x=567"), so modifing for {X}{Y} would not possible for this case – Vinicius Konig Ferreira Jan 15 '22 at 22:39
  • It can be a typing problem. Try wrapping them into a string! `id="1234"&x="567"` ... see https://stackoverflow.com/questions/49741284/asp-net-core-fromquery-usage-and-url-format – Isolin Jan 15 '22 at 22:43
  • For now I will just use the old httpClient... at least this one works fine :( – Vinicius Konig Ferreira Jan 15 '22 at 22:44

1 Answers1

8

https://github.com/reactiveui/refit#dynamic-querystring-parameters

You can either use a custom POCO class with the parameters you need, or you can use a Dictionary<string,string> to pass the values. The dictionary seems to best fit your usecase.

public class MyQueryParams
{
    [AliasAs("order")]
    public string SortOrder { get; set; }

    public int Limit { get; set; }

    public KindOptions Kind { get; set; }
}

public enum KindOptions
{
    Foo,

    [EnumMember(Value = "bar")]
    Bar
}


[Get("/group/{id}/users")]
Task<List<User>> GroupList([AliasAs("id")] int groupId, MyQueryParams params);

[Get("/group/{id}/users")]
Task<List<User>> GroupListWithAttribute([AliasAs("id")] int groupId, [Query(".","search")] MyQueryParams params);


params.SortOrder = "desc";
params.Limit = 10;
params.Kind = KindOptions.Bar;

GroupList(4, params)
>>> "/group/4/users?order=desc&Limit=10&Kind=bar"

GroupListWithAttribute(4, params)
>>> "/group/4/users?search.order=desc&search.Limit=10&search.Kind=bar"

A similar behavior exists if using a Dictionary, but without the advantages of the AliasAs attributes and of course no intellisense and/or type safety.

edit: You can probably use HttpUtility.ParseQueryString to parse your string into a NameValueCollection, which is basically a Dictionary.

Alex AIT
  • 17,361
  • 3
  • 36
  • 73
  • Hello Alex, thanks for the help! So, I just tested this way and it works! Thanks! Still, the bigger issue here is that we need to pass values as objects "Prop1:Value1, Prop2:Value"... the endpoint that im trying to reach isnt in my domain, so I dont have sure that the name of the prop will be "Prop1,Prop2"... As you said, with the Dictionary it works, but I would have to parse the string ("id=1234&x=567") to be like Key:id Value:1234 ---- Key:x Value:567 the way I see, it kind of sucks... should be easier as the exemple I tried before ([Get("/click?{parm}")]) :( – Vinicius Konig Ferreira Jan 16 '22 at 10:46
  • You can probably use `HttpUtility.ParseQueryString` to parse your string into a NameValueCollection, which is basically a Dictionary. Overall you are using less and less of the core features of Refit, so the default HttpClient might also be viable. – Alex AIT Jan 16 '22 at 11:23