1

I have to send a request object via Refit which contains 2 IEnumerable and one string, but for some reason I can't send the object forward.

I've tried to use all the paramets from the interface. Ex: [Query(CollectionFormat.Csv)] or Multi / Pipes but no success.

I've also tried to create my own CustomUrlParameterFormatter but unfortunately here I'm stuck, because I don't see a good way to retrieve the name of the property from the object request that I'm sending.

The code for CustomUrlParameterFormatter

public class CustomUrlParameterFormatter : IUrlParameterFormatter
    {
        public string Format(object value, ParameterInfo parameterInfo)
        {
            if(value is IEnumerable enumerable)
            {
                var result = ToQueryString(enumerable, parameterInfo.Name);
                return result;
            }

            return string.Empty;
        }

        public static string ToQueryString(IEnumerable query, string parameterName)
        {
            var values = query.Cast<object>().Select(ToString).ToArray();
            var separator = parameterName + "=";

            return values.Any() ? separator + string.Join("&" + separator, values) : "";
        }

        public static string ToString(object value)
        {
            var json = JsonConvert.SerializeObject(value).Replace("\\\"", "\"").Trim('"');

            return Uri.EscapeUriString(json);
        }


    }   

The Call from the IService that I'm using

        [Get("/TestMethod")]        
        Task<HttpResponseMessage> TestMethod([Query]TestRequestDTO requestDTO, [Header("X-Correlation-ID")] string correlationId);

The Request object

    public class TestRequestDTO
    {   
        public IEnumerable<long> EnumOne { get; set; }
        public IEnumerable<long> EnumTwo { get; set; }
        public string MethodString { get; set; }
    }

Also the RefitClient configuration

var refitSettings = new RefitSettings();
            refitSettings.UrlParameterFormatter = new CustomUrlParameterFormatter();

services.AddRefitClient<IService>(refitSettings)
                .ConfigureHttpClient(c => c.BaseAddress = new Uri(settings.Services.IService));

What I'm trying to achieve is something like

TestMethod?EnumOne =123&EnumOne =321&EnumTwo=123&EnumTwo=321&methodString=asdsaa

and instead I'm receiving other behavior without CustomUrlParameterFormatter()

TestMethod?EnumOne=System.Collections.Generic.List`1%5BSystem.Int64%5D&EnumTwo=System.Collections.Generic.List`1%5BSystem.Int64%5D&MethodString=sdf
Mircea Mihai
  • 167
  • 1
  • 13
  • Is there any demo to reproduce your issue? Share us the code which you used to send request with `RefitClient`. – Edward Jun 05 '19 at 01:53
  • Hi @TaoZhou I've attached a sample project -> https://github.com/moisoiu/refit – Mircea Mihai Jun 05 '19 at 05:21
  • I think currently it is not supported, you could trace this issue [[QueryParameters] Are broken for classes with IEnumerable properties #587](https://github.com/reactiveui/refit/issues/587) – Edward Jun 05 '19 at 08:02
  • I was afraid of that to be honest, but I thought maybe I'm blind and I'm doing something wrong. Will check the opinion of the guys from Refit also. Thanks – Mircea Mihai Jun 05 '19 at 08:23

0 Answers0