Warm tip: This article is reproduced from serverfault.com, please click

Python post to ms teams

发布于 2020-12-06 16:31:21

Trying to post a message to MS Teams webhook from Python below:

pmr = urllib3.PoolManager()
text='hello world'
message = {"Test":text}
enco_message = json.dumps(message).encode('utf-8')
r=pmr.request('POST',url, headers={'Content-Type': 'application/json'}, body=enco_message)
print(r.status)

But this does not send any message to teams and returns response status 400. Not sure what is to be changed in the code. Thanks for the help.

Questioner
omdurg
Viewed
33
omdurg 2020-12-07 15:12:28

This worked by explicitly specifying text keyword in the json, like below:

pmr = urllib3.PoolManager()
msg='hello world'
message = {"text":msg}
enco_message = json.dumps(message).encode('utf-8')
r=pmr.request('POST',url, headers={'Content-Type': 'application/json'},
body=enco_message)

The payload only works with the keyword text.