vrund297
vrund297

Reputation: 29

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 "}}

Upvotes: 1

Views: 1817

Answers (2)

sdo
sdo

Reputation: 795

The same response (HTTP status code 400 and {"error":{"code":"UnableToDeserializePostBody","message":"were unable to deserialize "}}) occurs when you set an unexpected Content-Type header. If you set one, it should be "application/json".

Upvotes: 0

Shiva Keshav Varma
Shiva Keshav Varma

Reputation: 3595

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.

Upvotes: 4

Related Questions