1

I'm trying to post to a hipchat room via Python and the v2 API.

I can post without problems via curl in a shell script:

ROOM_ID=123
AUTH_TOKEN=123456789
MESSAGE="Hello World"
curl -H "Content-Type: application/json" \
     -X POST \
     -d "{ \"from\": \"GTM\",
           \"notify\": \"true\",
           \"color\": \"red\",
           \"message_format\": \"text\",
           \"message\": \"$MESSAGE\"
         }" \
     https://hipchat.server.domain.com/v2/room/$ROOM_ID/notification?auth_token=$AUTH_TOKEN

However sending a message with the same payload via Python fails. I have used ready-made clients as well as simple requests via various http modules, and examples like this: https://gist.github.com/bdclark/4bc8ed06643e077fa620 (also of course I searched SO itself and tested examples like this one).

As a basic example I tried e.g. this:

host = 'hipchat.host.domain.com'
room = '123'
message = "Hello World"
headers = {'Content-type: application/json'}
color = "yellow"
format = "text"
notify=False
AUTH_TOKEN="123456789"

url = "https://{0}/v2/room/{1}/notification?auth_token={2}".format(host, room, AUTH_TOKEN)
h = httplib2.Http()
payload = {
    'from':'FROM',
    'message': message,
    'notify': notify,
    'message_format': format,
    'color': color
}
resp, content = h.request(url,"POST",urllib.urlencode(payload),headers=headers)

httplib2 (and clients based on it) return a responseNotReady error. requests (and clients based on it) return a Connection reset by peer error.

Since Curl sends without problems it's probably not an issue with Hipchat itself. I assume that there might be an problem with my Python installation (this is the default 2.7 on MacOs Sierra). So the question would be, how do I find the underlying cause for the errors.

Any help much appreciated.

Community
  • 1
  • 1
Eike Pierstorff
  • 31,996
  • 4
  • 43
  • 62
  • Can you post the python code you are using as well? – chaos Apr 20 '17 at 13:51
  • @chaos, I used a rather broad variety (e.g. the code from the links), so it's not a specific implemementation that fails. The only thing that worked so far was pyCurl, and I would much prefer a python-only solution. – Eike Pierstorff Apr 20 '17 at 13:59
  • 1
    Which OS are you using, and which version of Python? Do you receive any warnings? The problem may be with the SSL implementation. I just stepped on this under Ubuntu 14.05 LTS. If you use `requests`, [be sure](http://stackoverflow.com/questions/29099404/ssl-insecureplatform-error-when-using-requests-package) to `pip install requests[security]` (and its required OS packages). – 9000 Apr 20 '17 at 14:45

0 Answers0