Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

REST request cannot be encoded for GET (URL too long)

Tags:

Example:

  • URL: http://example.com/collection/a%20search%20term

  • Method: GET

  • Response: All items in collection matching a search term.

Problem: The search term may be so long that it breaks the web server's maximum URL length.

How do I allow extremely long search terms and still stay RESTful?

like image 284
feklee Avatar asked Nov 04 '13 15:11

feklee


People also ask

How long can a REST API URL be?

The REST API supports Uniform Resource Locators (URLs) with a length of up to 6000 characters. To avoid exceeding this limit, it is important to be aware of URL encoding.

How long is too long for an API request?

A Diffbot Extract API request will timeout after a maximum of 180 seconds (3 mins). If a request reaches 180 seconds, it will be automatically timed-out and an error returned.

How do I pass multiple query parameters in REST URL?

Query parameters are passed after the URL string by appending a question mark followed by the parameter name , then equal to (“=”) sign and then the parameter value. Multiple parameters are separated by “&” symbol.

Can we send body GET request?

So, yes, you can send a body with GET, and no, it is never useful to do so. This is part of the layered design of HTTP/1.1 that will become clear again once the spec is partitioned (work in progress). Yes, you can send a request body with GET but it should not have any meaning.


2 Answers

For inspiration, I just looked at Google Translate's API v2, which is "using the RESTful calling style."

Naturally, texts to be translated can be quite long. And so Google optionally allows sending a request with POST, but with a twist:

To use POST, you must use the X-HTTP-Method-Override header to tell the Translate API to treat the request as a GET (use X-HTTP-Method-Override: GET).

So it is possible to semantically transform a POST request into a GET request.

(This discovery led me to add the x-http-method-override tag to my question.)

like image 198
feklee Avatar answered Sep 21 '22 04:09

feklee


REST does not restrict POST to creation. Be careful with mapping CRUD to HTTP methods and assume that's RESTful. POST is the method used for any action that isn't standardized by HTTP.

Since the standard doesn't establish a limit for URIs, this can be considered a broken implementation, and it's ok to fix it. As long as the workaround is loosely coupled to your API, you are still RESTful. This means your API shouldn't implement a translation or override directly, but on a pre-processor of some kind that rewrites the request properly. It should be clearly documented somewhere that this is due to a broken implementation, and you expect it to eventually become obsolete.

like image 21
Pedro Werneck Avatar answered Sep 21 '22 04:09

Pedro Werneck