Skip to content

Commit

Permalink
Fix Mattermost alert webhook call (#5700)
Browse files Browse the repository at this point in the history
Mattermost seems to have gotten more particular about the `Content-Type` 
header of incoming webhooks in the upgrade from 5.18 to 5.22.

We were seeing errors of this kind:

```json
{
  "level": "error",
  "ts": 1588614108.121396,
  "caller": "mlog/log.go:175",
  "msg": "Could not decode the multipart payload of incoming webhook.",
  "path": "/hooks/<hook_id>",
  "request_id": "wsb5g1xiqine7knmgj9yo9887w",
  "ip_addr": "<ip>",
  "user_id":"",
  "method": "POST",
  "err_where": "incomingWebhook",
  "http_code": 400,
  "err_details": "mime: no media type"
}
```

This change switches our alert webhook's `requests.post` call from using 
Requests' `data` param to using its `json` param, which sets the 
`Content-Type` to `application/json`, as Mattermost now requires.

See: 
https://requests.readthedocs.io/en/master/user/quickstart/#more-complicated-post-requests
  • Loading branch information
Scott Cranfill committed May 5, 2020
1 parent bf4aabc commit deb6632
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
3 changes: 1 addition & 2 deletions cfgov/alerts/mattermost_alert.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import json
import os

import requests
Expand All @@ -24,6 +23,6 @@ def post(self, text):
}
resp = requests.post(
self.webhook_url,
data=json.dumps(payload),
json=payload,
)
resp.raise_for_status()
9 changes: 5 additions & 4 deletions cfgov/alerts/tests/test_mattermost_alert.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# -*- coding: utf-8 -*-
import json

from django.test import TestCase

Expand All @@ -24,7 +23,9 @@ def test_post(self, mock):
MattermostAlert(credentials, icon_url=icon).post(text)
mock.assert_called_once_with(
webhook_url,
data=json.dumps({'text': text,
'username': username,
'icon_url': icon})
json={
'text': text,
'username': username,
'icon_url': icon
}
)

0 comments on commit deb6632

Please sign in to comment.