Search code examples
c#odata

How to shorten Odata query string?


I have a method that queries an api using policy numbers. I found that the character limit for a query string is just over 2k. Here is my method that sends the query. How can I ensure or shorten the string so that it never goes above the character limit?

 public void BuildPolicyQuery()
        {
            if (!PolicyNumbers.IsAny()) return;
            //if (PolicyNumbers == null || PolicyNumbers.Count() == 0) return;
            QueryForWebServices = "Policy?$filter=PolicyNumber eq '" + PolicyNumbers.First() + "'";
            if (PolicyNumbers.Count() == 1) return;
            foreach (var p in PolicyNumbers.Skip(1))
            {
                QueryForWebServices += " or PolicyNumber eq '" + p + "'";
            }
        }

Solution

  • If you can modify the service, then you could define an action on the Policy route and pass the policy numbers in the action payload. For example:

    POST /Policy/SomeNamespace.GetSubsetByPolicyNumber
    { "PolicyNumbers": [ 123, 456, ... ] }
    

    You'd want an action rather than a function because the former uses POST and takes a payload (content body) while the latter uses GET with parameters in the URI.

    Actions and Functions in OData v4 Using ASP.NET Web API 2.2