Search code examples
pythonslack

Slack Webhook - returning invalid_payload


I'm trying to send data to a slack webhook but I keep getting an invalid_payload response.

My results variable from below looks like this if i print it in my script:

{u'results': [{u'TunnelID': 11111}]}

webhook_url = 'https://hooks.slack.com/services/xxx/xxx/xxx'

response = requests.post(
    webhook_url, data=json.dumps(results),
    headers={'Content-Type': 'application/json'}
)

if response.status_code != 200:
    raise ValueError(
        'Request to slack returned an error %s, the response is:%s'
        % (response.status_code, response.text)
    )

I'm sure its a problem with the way my results variable is formatted, but I can't seem to find the right way to format it.


Solution

  • Perhaps you mean to include results as the message itself? In which case, something like this?

    response = requests.post(
        webhook_url, json={'text': str(results)},
        headers={'Content-Type': 'application/json'}
    )