Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Having problems with Post via python

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()
like image 607
Fishy Avatar asked Dec 11 '25 05:12

Fishy


1 Answers

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()
like image 70
Fishy Avatar answered Dec 13 '25 13:12

Fishy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!