首页 > 解决方案 > Getting clean mail data from Microsft Graph API

问题描述

I have been trying to get clean mail data from Microsoft Graph API into a Django template.

I have tried using

headers={'Prefer': 'outlook.body-content-type="text"'}

and

headers={'Prefer': 'outlook.body-content-type="html"'}

in my python GET request but both of them result in data with tags in them. I have tried using regex to clean my data but it is not as effective because regex removes some important texts.

Is there a simpler way of getting a cleaner mail data?

标签: pythondjangogetmicrosoft-graph-api

解决方案


您需要使用消息的 body 属性来获取其是否为 HTML/Text。您可以使用首选项标题来获取它。当您指定任一标头时,成功的响应将包括相应的 Preference-Applied 标头,

  • 对于文本格式请求:Preference-Applied:outlook.body-content-type="text"
  • 对于 HTML 格式请求:Preference-Applied:outlook.body-content-type="html"

如果正文是 HTML,默认情况下,Outlook 会在返回 REST 响应中的正文内容之前删除嵌入在正文属性中的任何可能不安全的 HTML(例如 JavaScript)。

  • 要获取完整的原始 HTML 内容,请包含“Prefer: outlook.allow-unsafe-html” HTTP 请求标头
  • 除了上述之外,您还可以通过 $value 获取消息的 MIME。
  • 此外,您最终检索了 HTML 数据,然后使用 Beautiful Soup 清除了标签。

推荐阅读