2

I'm trying create a draft (or send a message) with attachment to gmail using its API. I've read some answers and tried to built the request according to what I've read here: Mail attachment wrong media type Gmail API

Before coding the function itself, I decided to use a Chrome extension (Simple Rest Client) to simulate the API request. Here's the request body:

Content-Type: multipart_mixed; boundary="foo_bar_baz"
MIME-Version: 1.0
to: receiver@gmail.com
from: sender@gmail.com
subject: Testing Subject

--foo_bar_baz
Content-Type: text/plain; charset="UTF-8"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit

This is the testing text

--foo_bar_baz

Content-Type: image/jpeg
MIME-Version: 1.0
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="test.jpg"

{
"message":
{
"raw" :     "_9j_4AAQSkZJRgABAQEAYABgAAD_2wBDAAIBAQIBAQICAgICAgICAwUDAwMDAwYEBAMFBwYHBwcGBwcICQsJCAgKCAcHCg0KCgsMDAwMBwkODw0MDgsMDAz_2wBDAQICAgMDAwYDAwYMCAcIDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAz_wAARCAAJAAsDASIAAhEBAxEB_8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL_8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4-Tl5ufo6erx8vP09fb3-Pn6_8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL_8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3uLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3-Pn6_9oADAMBAAIRAxEAPwD9Pfiv-wN4q8cftk3Pji30_wCH9z9v8V6H4ksPiFe3cy-MvAunaeuni68N6bCLR92n3_2G8ErLf2yAeIL_AHW021xdfX9FFAH_2Q**"
}
}

--foo_bar_baz--    

The request header parameters are as follows:

Authorization: Bearer *given token*
Content-Type: multipart/mixed; boundary="foo_bar_baz"
Content-Length: 1428

As you can see, it's pretty similar to the example in the link above. However, I keep getting the following response:

"message": "Media type 'application/octet-stream' is not supported. Valid media types: [message/rfc822]"

I know the API docs say the only valid media type is message/rfc822 (https://developers.google.com/gmail/api/v1/reference/users/drafts/create). Nonetheless, this sample (https://developers.google.com/gmail/api/guides/uploads#multipart) and others here in Stackoverflow say otherwise. The author of the question in the link above seem to have solved his problem without using message/rfc822 media type.

I gotta be missing something. Can someone help me with this? I'd really appreciate if someone could help me figure it out.

Geppetto
  • 23
  • 1
  • 3
  • 2
    Hi Geppetto. Could you please share your final POST-format of when you got it to work? Struggling with this myself. Thanks! – Tholle Mar 03 '15 at 09:53

1 Answers1

3

OK, so if you're using the /upload media feature (works for all messages irregardless of size) then for example it should be something like the following (and looks like i was a bit mistaken):

POST https://www.googleapis.com/upload/gmail/v1/users/me/messages/send
Content-Type: multipart/related; boundary=foo_bar_baz

then your POST body should be something like the following (not encoded, etc):

--foo_bar_baz
Content-Type: application/json; charset=UTF-8

{
}

--foo_bar_baz
Content-Type: message/rfc822
MIME-Version: 1.0
to: receiver@gmail.com
from: sender@gmail.com
subject: Testing Subject

--foo_bar_baz
Content-Type: text/plain; charset="UTF-8"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit

This is the testing text

--foo_bar_baz

Content-Type: image/jpeg
MIME-Version: 1.0
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="test.jpg"




--foo_bar_baz--

So things to note are that it's actually "multipart/related" and that has a application/json (for some requests you can add parameters there) part as well as a message/rfc822 part that contains the entire email.

It's not easy for sure--libraries definitely make it less painful if you can use them!

Eric D
  • 6,901
  • 1
  • 15
  • 26
  • Hi, @EricDeFriez. Thanks for the answer. I'm posting directly to https://www.googleapis.com/upload/gmail/v1/users/me/drafts?uploadType=multipart. So, should I include both the message and the attachment in the _raw_ parameter? How do I do this? Where do I include the filename, for example? – Geppetto Oct 30 '14 at 20:10
  • Got it! I was surely mixing up lots of different stuff. Cooled down, read a bit and figured it out. Your tip of setting "Content-Type: message/rfc822" on the POST header was crucial. I haven't read it anywhere but here. Thanks a lot. I'll soon post my final HTTP POST format, so others can benefit of it too. Again, thanks, your answer was very time saving. – Geppetto Oct 30 '14 at 21:12
  • @Geppetto, although it is 3 years later, I eagerly await your final HTTP POST format comment. The documentation hasn't gotten much better since you were working this. If you have it around, I'd love to see it. Thanks! – Bryan Aug 26 '17 at 19:32
  • Use the message structure that [Tholle](https://stackoverflow.com/users/3617886/tholle) posted [here](https://stackoverflow.com/questions/24908700/mail-attachment-wrong-media-type-gmail-api), set the Content-Type header to message/rfc822, and most importantly send the message as is in the body, no base64 encoding. This worked for me with the uploadtype header set to media. I hope you see this @Bryan – Hatch Aug 26 '17 at 20:43