tags:

views:

163

answers:

4

I've seen many code fragments that base64 encode images before transmitting over HTTP protocol.

I am wondering why do we need it?

A: 

The HTTP protocol isn't guaranteed to be "8 bit clean", so it might mangle a binary stream.

Paul Tomblin
can you please explain how can HTTP be not 8 bit clean? How will server know? Is it specified in request headers?
Andrey
This is just wrong. You can send whatever in the HTTP body
Martin Wickman
@wic, can you show me where in the HTTP specification it guarantees that the connection is 8 bit clean, because I can't find it.
Paul Tomblin
RFC 2616, Section 7.2 (http://greenbytes.de/tech/webdav/rfc2616.html#rfc.section.7.2)
Julian Reschke
@Julian, I don't see any such guarantee there, or anywhere else in the document for that matter. As a matter of fact, the Transfer-Encoding section appears to be there precisely *because* there is no such guarantee.
Paul Tomblin
Well, you are mistaken. HTTP is 8 bit clean; the message body simply carries a set of OCTETs (7.2).
Julian Reschke
The default fallback encoding is `binary/octet-stream` (section 7.2.1). Anyway, this discussion is pretty academic since 99.9999% of all HTTP content is sent as binary anyway (standard or not).
Martin Wickman
+1  A: 

Have a look at:

http://stackoverflow.com/questions/201479/what-is-the-use-of-base-64-encoding

karim79
+1 The accepted answer to the posted question is a great answer.
Matt Davis
+1  A: 

It's not necessary, but it enables you to embed images without performing additional HTTP requests (where, in some cases, it's not possible or permitted).

Adrian
Using the data URI scheme, you mean?http://en.wikipedia.org/wiki/Data_URI_scheme
David Gelhar
A: 

From the Wikipedia entry on Base64:

The term Base64 refers to a specific MIME content transfer encoding. It is also used as a generic term for any similar encoding scheme that encodes binary data by treating it numerically and translating it into a base 64 representation. The particular choice of base is due to the history of character set encoding: one can choose a set of 64 characters that is both part of the subset common to most encodings, and also printable. This combination leaves the data unlikely to be modified in transit through systems, such as email, which were traditionally not 8-bit clean.

And specifically regarding HTTP:

Base64 encoding can be helpful when fairly lengthy identifying information is used in an HTTP environment. For example, a database persistence framework for Java objects might use Base64 encoding to encode a relatively large unique id (generally 128-bit UUIDs) into a string for use as an HTTP parameter in HTTP forms or HTTP GET URLs. Also, many applications need to encode binary data in a way that is convenient for inclusion in URLs, including in hidden web form fields, and Base64 is a convenient encoding to render them in not only a compact way, but in a relatively unreadable one when trying to obscure the nature of data from a casual human observer.

Matt Davis
Your quote is great! But it doesn't explain why transfer images in base64?
Andrey