Nader
Nader

Reputation: 5595

How can I shorten a string and later get back the original contents?

I have a really long string that I need to pass in a URL, say 10,000 characters. Anyone know a good way to shorten it to under 2,000 chars, and then on a server somehow get the original back?

This is Objective-C talking to Ruby, but that shouldn't matter.

Upvotes: 2

Views: 705

Answers (6)

Matthew Vines
Matthew Vines

Reputation: 27561

I would persist this information to a database (or any other persistence source) and then pass a reference to it in the URL.

Both source and destination will require access to the database, but that isn't an issue most of the time.

Upvotes: 0

Daniel Brückner
Daniel Brückner

Reputation: 59655

Well, compress it and Base64 encode the result. If the string has a very specific format, a custom encoding could even yield a better compression. Can you give an example?

Upvotes: 0

Buggabill
Buggabill

Reputation: 13901

Store it in a database and then just pass the id of the string in the url.

Upvotes: 1

John Millikin
John Millikin

Reputation: 200826

You can try running it through Base64. If the string is guaranteed to have only a subset of possible characters -- for example, [a-zA-Z0-9] -- it can be shortened even more by converting these to unique ordinals and using a higher base encoding.

But it would probably be easier to just use POST.

Upvotes: 0

Michael Stum
Michael Stum

Reputation: 180954

Are you sure you have to pass it in as a URL? Maybe POST-Data or Session would be more appropriate? otherwise you could store the string in a database and return the key of the inserted record as a URL Parameter. If this is a security concern (as people can just change the number if it is an integer key), you could create a UUID as key.

Upvotes: 1

Byron Whitlock
Byron Whitlock

Reputation: 53871

Can you post the data? If you use GET the max length of a url is around 4000 characters. If you POST it you have no constraints (except timeouts memory etc)

This article talks about doing a post from objective-c

Upvotes: 5

Related Questions