Have you ever needed to make an http call and had to assemble a giant query string entirely manually?
I hope you never have to. 😆
It's a simple library that allows you to turn a class into a query string for https calls.
The implementation is available through a static class QueryStringComposer
.
The two available overloads perform the same conversion.
You can use the method Configure
provided by QueryStringComposerConfiguration
in your service configuration to change the key case style globally.
QueryStringComposerConfiguration.Configure(options =>
{
options.KeyNameCaseStyle = StringCaseStyle.TrainCase;
});
There are some supported style cases, they are:
public enum StringCaseStyle
{
CamelCase = 1,
PascalCase = 2,
SnakeCase = 3,
KebabCase = 4,
TrainCase = 5
}
Code result: http://localhost?SomeName=Victor&SomeAge=20
const string baseUrl = "http://localhost";
var queryObject = new YourClass
{
SomeName = "Victor",
SomaAge = 20
};
var result = QueryStringComposer.Compose(baseUrl, queryObject);
Code result: http://localhost?SomeName=Victor,Juan&SomeAge=20,21
var uri = new Uri("http://localhost");
// Some Uri Changes
var queryObject = new YourClass
{
SomeNames = new List<string> { "Victor", "Juan" },
SomaAges = new List<int> { 20, 21 }
};
var result = QueryStringComposer.Compose(uri, queryObject);
In case you need to pass a custom value as a key and don't want to mess up your code with non-standard names. You can use the QueryStringKeyNameAttribute
attribute for this.
Code result: http://localhost?user_name=Jorge
const string baseUrl = "http://localhost";
var queryObject = new YourClass
{
UserName = "Jorge",
};
var result = QueryStringComposer.Compose(uri, queryObject);
class YourClass
{
[QueryStringKeyName("user_name")]
public string UserName { get; set; }
}
In case you only need to ignore one property and don't want to create a new class for it. You can use the QueryStringIgnoreAttribute
attribute for this.
Code result: http://localhost?Login=victorvhn
const string baseUrl = "http://localhost";
var queryObject = new YourClass
{
Login = "victorvhn",
Password = "d74ff0ee8da3b9806b18c8"
};
var result = QueryStringComposer.Compose(uri, queryObject);
class YourClass
{
public string Login { get; set; }
[QueryStringIgnore]
public string Password { get; set; }
}
In case you need to change the key case style of a single property and don't want to change it globally. You can use the QueryStringKeyCaseStyleAttribute
attribute for this.
Code result: http://localhost?Key-In-Train-Case=value
const string baseUrl = "http://localhost";
var queryObject = new YourClass
{
KeyInTrainCase = "value",
};
var result = QueryStringComposer.Compose(uri, queryObject);
class YourClass
{
[QueryStringKeyCaseStyleAttribute(StringCaseStyle.TrainCase)]
public string KeyInTrainCase { get; set; }
}
Some types are not supported for conversion:
- Complex Lists.
- Complex Dictionaries.
Providing a dictionary to compose, the values will not be converted.
Code result: http://localhost?key1=value1&key2=value2
const string baseUrl = "http://localhost";
var dic = new Dictionary<string, string>
{
{ "key1", "value1" },
{ "key2", "value2" }
};
var result = QueryStringComposer.Compose(baseUrl, dic);