HTTP GET request with many items in query string
We make an HTTP GET request with a lot of data in the query string, representing all of the ids of a collection to retrieve.
Regarding the limit on the length of the query string in the url, a quick google search says:
RFC 3986 also states there is no limit, but indicates the hostname is limited to 255 characters because of DNS limitations (section 2.3.3). Microsoft states that the maximum length of a URL in Internet Explorer is 2,083 characters, with no more than 2,048 characters in the path portion of the URL.May 1, 2009
If we don't use IE, should I be concerned about potentially exceeding the limit on the query string length? I am certain I have seen the limit exceeded on my Node.js Express server, specifically when I included a base64 string representing an image in the query string of a GET request.
What's a good way to get around this problem? Should we just use an HTTP POST request instead? Certainly we do not want to break apart 1 GET request into 1000's just to avoid this problem.
If you intend to keep your API clean and RESTful, you could split your request into two parts. The first one will send your configuration to the server for calculations (POST); the second one will collect the results (GET).
This approach has multiple advantages except for overcoming the limit of HTTP GET on browser, firewall, routers, etc. For example, you can later introduce partial result polling or a possibility to cancel a long running calculation. Further more, you keep the URL short and user-readable.
The implementation details were already described here.
In a RESTful web service, GET retrieves data. POST requests create data on the server and do not have size limits, but servers often have a default limit that can be changed.
If maintaining a RESTful application does not apply to your situation, then you can use POST.
It is important to note that GET requests can be cached but POST requests cannot, so if you are getting a lot of (the same) data in return, you should use GET.
I would say that if you have GET
request with a length over 2000 than it smells like something is wrong in the design (I can't find a good reason to pass an image via GET in base64 instead of POST as you mentioned). I would rather go with POSTs in that scenarios; also searching around about the 2048 characters limit seems it does not really apply, see firefox and Chrome and i won't rely on this dated info about microsoft ie, here a more detailed analysis.
You can also consider to implement a string shortener for your parameters names and values but in my opinion readability is more important. Have a look at this question on how to shorten parameter in javascript.
链接地址: http://www.djcxy.com/p/42176.html上一篇: 可能URL中包含GET中的任意二进制数据