Building a repeatable HTTP request parameter
What I am facing:
I am building a search application that involves several search parameters and I would like to be able to bookmark the URL for later usage.
I imagine the solution would something like the following:
- User views the page, picks search parameters they want to filter.
- Browser do a POST request with the parameters.
- The application compiles/aggregates the parameters into one [A].
- The user can copy the URL with [A] appended and repeat the same search in the future.
To give a bit vision, both request below should be equal:
Original request:
POST /search (with POSTed parameters key1=asd and key2=10)
Subsequent/bookmarkable request:
GET /search?params=ABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890
params
should be reversible (and probably validated, but not crucial) to the original parameters.
My question is: what would be the best scheme to compile the parameters into one parameter?
What I have thought so far:
I am thinking about using key-value dictionary, comma separated, Base64 encoded; but I'm not sure how long the encod开发者_开发问答ed string would be (also keep in mind URL length limitation). What I'm probably looking for is probably a reversible compression. Any gotcha over this approach is welcome.
Apart from the POST/GET thing mentioned in the comments, what I do in PHP is:
a) Store the origional (validated) parameters in the session and pass a hash of them around (an md5 of the string of the params) or whatever. This is used for search-in-results type stuff. Obviously my examples here are primative, but will give you the idea. We store the params that led to the results so:
Request A)
search.php?a=b&c=d&e=f...
Hash generated by md5()
on the query string of the search:
$_SESSION['old_searches'][md5($_SERVER['QUERY_STRING'])] = $_GET;
Request B)
search.php?in=[hash of a=b&c=d used to lookup that query string]
Might be quite basic but I think this is the type of thing you are after? In PHP we do:
$old_params = $_SESSION['old_searches'][$_GET['in']];
Your hash can be anything, an integer, a datetime stamp, Unix epoch time...
If you want to make it bookmarkable then just have a method to store the serialized HTTP query string or POST input into a database along with a UID rather than a hash. In this situation a hash is only OK if the stored value always corresponds to the hash/is immutable without mutating the hash. I may use:
$database_key = generate_small_uid();
$database_value = json_encode($_POST);
printf('<a href="/search_bookmark/%s">Your search bookmark</a>', $database_key);
Just don't use an MD5 as primary key or anything like that. In this situation you just need a small unique identifier.
Obviously with safer handling. I'm sure someone better acquainted with C# will know the equivalent from PHP (basically it is just a hash value relating to state).
I think that either you need to use GET to make the whole mechanism stateless (so all the required information are encoded in the url), or keep the state on the server in some database and then use a key to that state.
I would strongly recommend going for the GET method, even though you have many parameters - it is the only robust and scalable way (e.g. take a look at any of the google services). To make your urls a bit more readable you could consider making some of the parameters part of the path instead of a paramter, i.e.
http://www.mysite.com/param1_value/param2_value/param3_value/search.aspx?param4=param4_value&....
I think this is better than trying to hash/compress these real parameter values, because this way browsers like Firefox or Chrome will enable the user to search on these strings while typing in the address bar.
精彩评论