通过python发布问题

Fis*_*shy 3 python post cgi http

我有点麻烦.我正在尝试发送一个POST并试图按照文档,但我似乎无法正确.

在github上:https://github.com/trtmn/Python

拉请求欢迎!

# 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()
Run Code Online (Sandbox Code Playgroud)

Fis*_*shy 5

看起来我需要将其字符串化为JSON(我知道,但不知道如何).感谢Tim G.的协助.

所以这是功能代码:

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()
Run Code Online (Sandbox Code Playgroud)