[英]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.
我正在尝试发送一个POST并试图按照文档,但我似乎无法正确。
on github: https://github.com/trtmn/Python 在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()
looks like I needed to stringify it as JSON (which I knew, but didn't know how). 看起来我需要把它串作为JSON(我知道,但不知道如何)。 Thanks to Tim G. for the assist.
感谢Tim G.的协助。
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()
Using httplib as an alternative for POST: 使用httplib作为POST的替代方法:
*import httplib *导入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. 看看这是否有帮助。
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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/ ' url =' https://hooks.slack.com/services/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/ '
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.