2014-11-05 2 views
2

약간의 문제가 있습니다. 나는 POST를 보내려고 노력하고있어 설명서를 따르려고하지만, 제대로 할 수없는 것 같습니다. GitHub의에Python을 통해 게시 할 때 문제가 발생했습니다.

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

풀 요청을 환영합니다! 이 문제를 해결하지만 난 당신의 URL의 끝에 슬래시를 추가하면 내가 코드를 실행할 때 응답을 수신 할 경우

# 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() 

답변

3

내가 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() 
:

그래서 여기에 기능 코드의

1

:

* 수입 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()* 

이 도움이되는지 확인합니다.

관련 문제