microsoftgraph / msgraph-sdk-android

Microsoft Graph SDK for Android! https://graph.microsoft.io
Other
51 stars 43 forks source link

Adding attachment to draft message - 422 Unprocessable Entity #72

Closed rafaltrzcinski closed 6 years ago

rafaltrzcinski commented 6 years ago

Trying to follow example of adding file attachment for created draft message. Getting the error: 422 : Unprocessable Entity

{                                                                                               
   "error": {                                                                                   
      "code": "UnprocessableType",                                                              
      "message": "Cannot process input of abstract type 'Microsoft.OutlookServices.Attachment'",
      "innerError": {                                                                           
         "request-id": "request_id_string",                                  
         "date": "date_string"                                                          
      }                                                                                         
   }                                                                                            
}

Attachment is created the same way as in code example, by FileAttachment object, all fields are filled the same way. Is there another way to send file attachment in the same POST where draft message is created?

caitlinrussell commented 6 years ago

Could you include the JSON request body as well?

rafaltrzcinski commented 6 years ago

Probably i see where problem is: in JSON request body field @odata.type is missing, but actually i set it by FileAttachment().oDataType = "microsoft.graph.fileAttachment" (or with # at the begging) so the request body looks like:

{
  "contentBytes":"base64 file content",
  "contentId":"some_id",
  "isInline":false,
  "name":"file.jpg"
}

when i send it by raw JSON with @odata.type it is created correctly - why setting oDataType to FileAttachment won't add @odata.type in request body?

caitlinrussell commented 6 years ago

OK, this is what I suspected. You are correct - the bug here is that there is a rule in the serializer to ignore @odata.type because it is a transient type. I will check in a fix shortly

rafaltrzcinski commented 6 years ago

@cbales thank you for quick response 💪 Is there way to put file attachment into message in the same POST that create draft/send message?

caitlinrussell commented 6 years ago

You're welcome; you lucked out as someone had reported the bug internally a few days ago and I had just fixed it and was getting ready to make the commit! Unfortunately, you cannot put the attachment in the same POST request. This would require you to POST a new collection page to the draft, which isn't a scenario supported by the SDK. This is why the sample doesn't simply use the SendMail endpoint, which would prevent needing to create a draft in the first place.