1

I have 2 rails applications, one in Rails 2 (client), and one in Rails 4 (API).

The issue is that sometimes I have GET queries with too long URI. API is raising ERROR WEBrick::HTTPStatus::RequestURITooLarge exception.

At the moment, I have only 3 solutions that I do not like:

  • Transform GET query to POST/PUT (any query having body in request). I don't like it because the meaning of "get" have no sense anymore. I don't put anything, don't post anything, I only get information. http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html says:

    The GET method means retrieve whatever information (in the form of an entity) is identified by the Request-URI. If the Request-URI refers to a data-producing process, it is the produced data which shall be returned as the entity in the response and not the source text of the process, unless that text happens to be the output of the process.

  • Change the MAX_URI_LENGTH default value (as http://kandadaboggu.com/post/40618627239/fixing-webrick-httpstatus-requesturitoolarge does). For me, it's just crappy, and doesn't prevent to be big enough.

  • Adding my params into headers like How do you add a custom http header? . For me, it's the less crappy solution. I don't know if it's standard, maintenable, and easily bring back for API.

Do you have any other solution ? If you haven't, whats the less crappy one ?

Community
  • 1
  • 1
pierallard
  • 3,326
  • 3
  • 21
  • 48

1 Answers1

1

You're only real limitation is the length of the URI in case you use the API URLs in a web browser (2,083 characters for Internet Explorer, KB208427).

If you are getting the WEBrick::HTTPStatus::RequestURITooLarge exception and you are under the 2,083 character limit, just increase the MAX_URI_LENGTH value. This constant exists primarily to prevent your application from being DOS'd into oblivion by sending large requests to it. You may be encountering a genuine need to increase this value.

If you are exceeding 2,083 characters for the URI length, then you probably need to refactor that service call into multiple service calls (which is not ideal) or you could look into changing the URL parameters to save on bytes.

For instance, let's say you need to supply a list of Ids for some sort of database relation:

foo[bar][0]=123&foo[bar][1]=535&foo[bar][2]=76456

This query string segment is 49 characters long. Compare that with:

foo[bar]=123,535,76456

Which is only 22 characters long giving you ~50% reduction in characters. You could look into micro optimizations like this if you see query string parameters that are repeated multiple times, or parameters with long names.

Greg Burghardt
  • 17,900
  • 9
  • 49
  • 92