11

Possible Duplicate:
What is the use of base 64 encoding?

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

I am wondering why do we need it?

Community
  • 1
  • 1
Pentium10
  • 204,586
  • 122
  • 423
  • 502

3 Answers3

2

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
  • 1,392
  • 9
  • 9
0

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
  • 45,297
  • 16
  • 93
  • 124
-1

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

Paul Tomblin
  • 179,021
  • 58
  • 319
  • 408
  • can you please explain how can HTTP be not 8 bit clean? How will server know? Is it specified in request headers? – Andrey Mar 10 '10 at 17:24
  • This is just wrong. You can send whatever in the HTTP body – Martin Wickman Mar 10 '10 at 17:29
  • @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 Mar 10 '10 at 17:43
  • RFC 2616, Section 7.2 (http://greenbytes.de/tech/webdav/rfc2616.html#rfc.section.7.2) – Julian Reschke Mar 10 '10 at 17:58
  • @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 Mar 10 '10 at 18:06
  • Well, you are mistaken. HTTP is 8 bit clean; the message body simply carries a set of OCTETs (7.2). – Julian Reschke Mar 10 '10 at 20:04
  • 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 Mar 10 '10 at 21:28