我现在有一个 heroku flask 应用程序。当它收到 POST 请求时,它使用 sendgrid API 将包含 POST 数据的电子邮件发送到我的个人电子邮件。相关代码(driver.sendgridemail 是用 https://github.com/sendgrid/sendgrid-python 编写的 python 的示例代码),其中有一个字符串作为参数传递:
@app.route('/', methods=['GET', 'POST'])
def index():
if request.method == "POST":
data = request.data.decode('utf-8')
driver.sendgridemail("Data is here: %s" % (data))
return data
else:
return render_template("index.html")
当我使用 requests.post 发布时:
requests.post("my_heroku_url", data=json.dumps({"some":"data"}), headers={"Content-Type":"application/json"})
我收到一封电子邮件到我的个人电子邮箱,其中包含“数据在这里:{"some":"data"}”
但是,当我使用 sendgrid 入站解析功能时,转发到我的个人电子邮件的唯一电子邮件是“数据在这里:”,这显然不理想。
它与内容类型有关吗?为什么它不处理来自传入电子邮件的数据?它显然正在接收电子邮件,因为它能够根据每个 flask 应用程序转发到我的个人电子邮件,它只是不检索数据。换句话说,POST 请求通过了,但它无法检索任何数据并给了我一个空白的二进制字符串。最后,仍然认为它是 b"是不是有点奇怪?这是否意味着它永远不会使用 sendgrid 解析解码为字符串,但它使用 python 请求发布?
请您参考如下方法:
根据docs :
The request that will be sent to the HTTP endpoint will be encoded as multipart/form-data.
您需要使用 request.form 字典来访问 Flask 中的表单数据。
Sendgrid 的博客上提供了教程:https://sendgrid.com/blog/collect-inbound-email-using-python-and-flask/
此外,当您进行更改时,您应该从 requests.post 调用中删除 headers={"Content-Type":"application/json"},这样您也可以将其作为表单数据发送。
