1 year ago
#331211
Renan Carvalho Ribeiro
Flurl http param naming strategy
Flurl doest not format query params based on newtonsoft json namin strategy like body content does, so I created a base class for translate it.
public abstract class FlurlBaseQueryParam
{
public Dictionary<string, string> ParseQueryParams()
{
var stringParams = JsonConvert.SerializeObject(this, settings: new JsonSerializerSettings()
{
NullValueHandling = NullValueHandling.Ignore
});
return JsonConvert.DeserializeObject<Dictionary<string, string>>(stringParams, new KeyValuePairConverter());
}
}
Then you can inherit and apply the naming strategy for each object as follows in the example below.
- CamelCaseNamingStrategy
- SnakeCaseNamingStrategy
- KebabCaseNamingStrategy
[JsonObject(NamingStrategyType = typeof(CamelCaseNamingStrategy))]
public class GetByParamRequest : FlurlBaseQueryParam
{
public string Identification { get; set; }
public string Email { get; set; }
}
And then you should call the ParseQueryParams method inside the SetQueryParams method, as follows.
public async Task<ResponseModel> GetByParamAsync(GetByParamRequest request)
{
try
{
return await Url.SetQueryParams(request.ParseQueryParams())
.GetJsonAsync<ResponseModel>();
}
catch (FlurlHttpException e)
{
}
return new ResponseModel();
}
c#
flurl
0 Answers
Your Answer