microsoft-graph-apimicrosoft-graph-mailmicrosoft-graph-calendar

Microsoft graph api attachment error of unable to deserialize


I have tried this i am able to create event but this error i am getting.

body_json={
        "@odata.type": "#microsoft.graph.fileAttachment",
        "name": "menu.txt",
        "contentBytes": "base64bWFjIGFuZCBjaGVlc2UgdG9kYXk="
    }



   API =" https://graph.microsoft.com/v1.0/users/{}/events/{}/attachments".format(userId,meetingID)
        body_json = json.dumps(body_json) 
        response = requests.request("POST", url=API, data=body_json, headers=self.headers)

error : <Response [400]> {"error":{"code":"UnableToDeserializePostBody","message":"were unable to deserialize "}}


Solution

  • The issue here is because the data which you are putting in the contentBytes property is not BASE64. As you are following the documentation they gave that example to make you understand that the data is base64. But actually in the example its not base64. So you can convert it to base64 online as below and test it.

    enter image description here

    This is the request body I used which has Hello World.

    {
        "@odata.type": "#microsoft.graph.fileAttachment",
        "name": "menu.txt",
        "contentBytes": "SGVsbG8gV29ybGQ="
    }
    

    Which gave me successful results in graph explorer. You can use it to test Graph API calls.