17

Browsers have limitation on the length of the URLs. IE has limitation that Url length should not exceed 2K characters.

When I form a $filter equals query, I could compare with multiple input values. In such a case the length of the Url would exceed 2K.

Does OData sets any limits on the length of the Url?

Thanks

Venki
  • 2,129
  • 6
  • 32
  • 54

3 Answers3

11

OData itself doesn't limit the length of the Url, but as you noted most clients and servers do. So usually it's a good practive to not produce URLs too long.

The problem you refer to (implementing the Contains operator, or something similar) has two possible workarounds:

1) Use service operation to handle such query for you. You can possibly pass the multiple input values encoded as a string or something like that, or maybe the service operation knows these up front anyway.

2) Use the long $filter, but send the request in a $batch request. The advantage is that the limit on the Url is much bigger and it's very unlikely you will hit it. The disadvantage is that even though you're trying to execute a GET request, due to the $batch it travels as POST request over the web and thus it won't be cached.

Vitek Karas MSFT
  • 13,130
  • 1
  • 34
  • 30
  • 1
    These are from the callstack from an exception thrown with "System.UriFormatException: 'Invalid URI: The Uri scheme is too long.':" As you see clearly Odata limits the uri just because it uses System.Uri and the limit is set in System.Uri.cs as c_MaxUriSchemeName = 1024. ` System.dll!System.Uri.CreateUri System.Web.OData.dll! ` – ozanmut Aug 08 '18 at 14:55
  • 1
    Would you mind updating your answer based on situation in 2021? – Maulik Modi Apr 19 '21 at 15:28
3

I defer to @Vitek's answer to the OP's question:

OData itself doesn't limit the length of the Url

But if others who arrive here because of IIS limitations : The request filtering module is configured to deny a request where the query string is too long., they may benefit from my answer. Follow that error's instructions:

Verify the configuration/system.webServer/security/requestFiltering/requestLimits@maxQueryString setting in the applicationhost.config or web.config file.

Follow the instructions:

<configuration>
  <system.webServer>
    <security>
      <requestFiltering>
        <requestLimits maxQueryString="50000">
        </requestLimits>
        ...

You may also get this error: The length of the query string for this request exceeds the configured maxQueryStringLength value.

This technique is described here and here, and it looks like this:

<configuration>
    <system.web>
        <httpRuntime maxQueryStringLength = "50000" ... />
Community
  • 1
  • 1
Nate Anderson
  • 18,334
  • 18
  • 100
  • 135
-2

I didn't find how the $batch is used. so I used $filter for sending a long request. Its pretty easy:

DataServiceQuery<CLIENT> ordersQuery = DataServiceQuery<CLIENT>)this.context.CLIENTS.AddQueryOption("$filter", MyFilter());

where MyFilter() return a string like this: "ID_CLIENT = 1 or ID_CLIENT = 2"

NB: Dont use the uppercase AND. it leads to and error. use and not AND

flup
  • 26,937
  • 7
  • 52
  • 74
ihebiheb
  • 3,673
  • 3
  • 46
  • 55