5

I'm writing a script that extracts images from email and uploads them to facebook. As a result, I'm getting image data encoded in base64, and I'd like to upload this data using curl without saving the image data to a file on disk.

Here is the body of my email:

Content-Type: multipart/mixed;
 boundary="------------030703050303060607000103"

This is a multi-part message in MIME format.
--------------030703050303060607000103
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

this message has a very small image of a single grey pixel attached to
it in jpeg format

--------------030703050303060607000103
Content-Type: image/jpeg;
 name="grey.jpg"
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
 filename="grey.jpg"

/9j/4AAQSkZJRgABAQEAAQABAAD/2wBDAP//////////////////////////////////////
////////////////////////////////////////////////wgALCAABAAEBAREA/8QAFAAB
AAAAAAAAAAAAAAAAAAAAAP/aAAgBAQAAAAF//8QAFBABAAAAAAAAAAAAAAAAAAAAAP/aAAgB
AQABBQJ//8QAFBABAAAAAAAAAAAAAAAAAAAAAP/aAAgBAQAGPwJ//8QAFBABAAAAAAAAAAAA
AAAAAAAAAP/aAAgBAQABPyF//9oACAEBAAAAEH//xAAUEAEAAAAAAAAAAAAAAAAAAAAA/9oA
CAEBAAE/EH//2Q==
--------------030703050303060607000103--

The following command works, but would require me to save the data to a file on disk (which I'm trying to avoid)

curl \
   -F 'source=@pixel.jpg' \
   -F 'message=this message has a very small image of a single grey pixel attached to it in jpeg format' \
   -F 'access_token=<secret>' \
   https://graph.facebook.com/<user_id>/photos

The following command does not work, but it's essentially what I want

curl \
   -F 'source=/9j/4AAQSkZJRgABAQEAAQABAAD/2wBDAP//////////////////////////////////////////////////////////////////////////////////////wgALCAABAAEBAREA/8QAFAABAAAAAAAAAAAAAAAAAAAAAP/aAAgBAQAAAAF//8QAFBABAAAAAAAAAAAAAAAAAAAAAP/aAAgBAQABBQJ//8QAFBABAAAAAAAAAAAAAAAAAAAAAP/aAAgBAQAGPwJ//8QAFBABAAAAAAAAAAAAAAAAAAAAAP/aAAgBAQABPyF//9oACAEBAAAAEH//xAAUEAEAAAAAAAAAAAAAAAAAAAAA/9oACAEBAAE/EH//2Q==' \
   -F 'message=this message has a very small image of a single grey pixel attached to it in jpeg format' \
   -F 'access_token=<secret>' \
   https://graph.facebook.com/<user_id>/photos

Obviously, the above command will not work. For one, I didn't specify the encoding of the data as base64. Is it possible to specify the encoding for just a single field or would I have to do it for the whole message (requiring me to encode the message and access_token fields as base64 too)?

In general, please tell me how to send a (base64 encoded) image via curl without first saving the data to a file on disk.

TIA

Michael Altfield
  • 2,083
  • 23
  • 39

1 Answers1

1

You could store it in a chunk of memory instead of file to disk: Example from libcurl

Also, you could try these options from curl (--help).

 -d, --data DATA     HTTP POST data (H)
     --data-ascii DATA  HTTP POST ASCII data (H)
     --data-binary DATA  HTTP POST binary data (H)
     --data-urlencode DATA  HTTP POST data url encoded (H)
     --egd-file FILE  EGD socket path for random data (SSL)
 -F, --form CONTENT  Specify HTTP multipart POST data (H)
     --form-string STRING  Specify HTTP multipart POST data (H)
     --ftp-account DATA  Account data string (F)