The Python Oracle

Office 365 REST API (Python) Mark Email as Read

--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Puzzle Island

--

Chapters
00:00 Office 365 Rest Api (Python) Mark Email As Read
00:45 Accepted Answer Score 2
01:10 Answer 2 Score 1
01:34 Thank you

--

Full question
https://stackoverflow.com/questions/2748...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #rest #python27 #office365

#avk47



ACCEPTED ANSWER

Score 2


At first glance it looks OK. I wonder if the Content-Type header isn't being set to "application/json" or something along those lines. Try getting a network trace and verify that the request looks something like:

PATCH https://outlook.office365.com/api/v1.0/Me/Messages('msgid') HTTP/1.1
Accept: application/json;odata.metadata=full
Authorization: Bearer <token>
Content-Type: application/json;odata.metadata=full
Host: outlook.office365.com
Content-Length: 24
Expect: 100-continue
Connection: Keep-Alive

{
  "IsRead": "true"
}



ANSWER 2

Score 1


Well I have an answer for myself and it is indeed a simple matter. It was a mistake to not fully read how PATCH is different from GET or POST. In short it's important to make sure your headers are set for the right content-type.

Here is the working code:

# once file acquired mark the email as read
changes = {u'IsRead':u'True'}
headers = {'Content-Type': 'application/json'}
json_changes = json.dumps(changes)
base_email_url = u'https://outlook.office365.com/api/v1.0/me/messages/{0}'.format( msgId )
response = requests.patch(base_email_url, data=json_changes, auth=__AUTH, headers=headers)
log.debug( response )