3

I'm having a bit of trouble. I'm trying to send a POST and trying to follow the documentation, but I can't seem to get it right.

on github: https://github.com/trtmn/Python

Pull requests welcomed!

# Getting documentation from :
#https://docs.python.org/2/howto/urllib2.html 
import urllib
import urllib2

url = 'https://hooks.slack.com/services/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'
values = {"username": "webhookbot", "text": "This is posted to #general and comes from a bot named webhookbot.", "icon_emoji": ":ghost:"}

data = urllib.urlencode(values)
req = urllib2.Request(url, data)
response = urllib2.urlopen(req)
the_page = response.read()
0

3 Answers 3

5

looks like I needed to stringify it as JSON (which I knew, but didn't know how). Thanks to Tim G. for the assist.

So here's the functional code:

import urllib2
import json

url = 'https://hooks.slack.com/services/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'
values = {"username": "webhookbot", "text": "This is posted to #general and comes from a bot named webhookbot.", "icon_emoji": ":ghost:"}

data = json.dumps(values)
req = urllib2.Request(url, data)
response = urllib2.urlopen(req)
the_page = response.read()
Sign up to request clarification or add additional context in comments.

Comments

1

Using httplib as an alternative for POST:

*import httplib

conn = httplib.HTTPSConnection(host)
conn.request('POST',urI,request_body, headers)    
response = conn.getresponse()
resp_status=response.status
resp_reason=response.reason
resp_body=response.read()
conn.close()*

See if this helps.

Comments

0

Not sure if this solves the problem but if I add a slash to the end of your url I do receive a response when I execute your code.

url = 'https://hooks.slack.com/services/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/'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.