When my browser downloads an image from a website, there is no base64 encoding. It issues an HTTP command, such as GET /image.jpg
, and receives an HTTP response whose Content-Type
is something like image/jpg
, whose Content-Length
is the number of bytes in the image and whose body is the data of the binary image itself . Data is not encoded using a character set or encoded using a scheme such as base64.
Writing RESTful resources taught me to expect symmetry between an HTTP GET
and a PUT
so that, for example, a URL that supplies JSON data when I do a GET
will receive JSON data when it is represented by a PUT
. In both cases, form coding is not involved; in both cases there is simply a Content-Length
, which indicates the number of bytes in the payload, a Content-Type
header declaring that the payload is JSON in a certain character set, and then the character data is separate and without any restrictions in quality GET
or PUT
bodies.
I am writing a PhoneGap application that allows users to take photos and upload them using my application. I expected that I could create a RESTful interface for this that supports symmetric GET
and PUT
- so that the PUT
commands do not contain special encoding and do not imply any representation of the character set, but simply have Content-Type
of image/jpg
and then a lot JPG binary data as their payload. Obviously, this is a more efficient use of bandwidth than trying to encode an image inside a form. And this approach works fine when I PUT
match the url with a tool like curl
.
But I was not lucky to make a clean RESTful PUT
from PhoneGap WebKit JavaScript! PhoneGap wants to return the image to my JavaScript as the local file:
URL, as well as the data:
URL, which contains the image data in strict accordance with base64 encoding. But in no case can I find a clear way to convert the image to pure binary format (could I use one of these new-fangled Blob
objects for this? If so, how?), And then call PUT
, which without decorating the request with additional form layers or cruft-coding, will simply transfer the raw image over the wire to my web server as an HTTP request payload.
Does anyone know how to induce WebKit to a PUT
AJAX request with a raw image as its body? Thanks for any pointers - or even any useful answers that I am approaching this, everything is wrong!
Brandon rhodes
source share