Invalid content type on user.message()
kevinlondon opened this issue · 4 comments
I'm trying to figure out how to send a user a message. In this case, I'm iterating through the list of users, picking out the user I'd like to message, and then sending a message with user.message("foo")
to test. Below is the py.test output from running the test (I've replaced the user ID with a stock one).
self = <hypchat._requests object at 0x103bd6860>, method = 'POST', url = 'https://api.hipchat.com/v2/user/12345/message', kwargs = {'data': '{"message_format": "text", "message": "foo", "notify": true}', 'headers': {'Content-Type': 'application/json'}}
rv = <Response [400]>
def request(self, method, url, **kwargs):
rv = super(Requests, self).request(method, url, **self._kw(kwargs))
# Raise one of our specific errors
if rv.status_code in _http_errors:
> raise _http_errors[rv.status_code](rv.text, response=rv)
E hypchat.requests.HttpBadRequest: {
E "error": {
E "code": 400,
E "message": "Invalid content type",
E "type": "Bad Request"
E }
E }
Do you have any suggestions as to what I might be doing wrong?
This sounds like a misconfiguration or bug with HipChat. The request posted matches the API docs.
I'm assuming that, since you've successfully enumerated users, you have a valid access token. Make sure it has the send_message
scope. (If it does not, HipChat sent the wrong HTTP error.)
If you can replicate the issue using eg cURL, might want to bounce this off HipChat support.
Ah, interesting. I got the error because I was storing my key in a file. There was a newline character at the end of my key which mattered when sending a message but did not matter when retrieving a list of users. rstrip("\n")
on the key fixed it. Thanks!
Might want to make your config parsing a bit more robust than that. I recommend one of the many structured file parsing libraries (ConfigParser, json, yaml, etc).
Yeah, good advice. Thanks again for the help.