【发布时间】:2012-04-24 02:57:24
【问题描述】:
我正在尝试找出 Intersango API 的正确 URL 格式(文档很少)。我正在用 C# 对我的客户端进行编程,但我正在查看 Python example,我对实际放置在请求正文中的内容有点困惑:
def make_request(self,call_name,params):
params.append(('api_key',self.api_key)) // <-- How does this get serialized?
body = urllib.urlencode(params)
self.connect()
try:
self.connection.putrequest('POST','/api/authenticated/v'+self.version+'/'+call_name+'.php')
self.connection.putheader('Connection','Keep-Alive')
self.connection.putheader('Keep-Alive','30')
self.connection.putheader('Content-type','application/x-www-form-urlencoded')
self.connection.putheader('Content-length',len(body))
self.connection.endheaders()
self.connection.send(body)
response = self.connection.getresponse()
return json.load(response)
//...
这段代码我看不懂:params.append(('api_key',self.api_key))
它是某种字典,被序列化为 JSON、逗号分隔的东西,还是它究竟是如何被序列化的?当参数被编码并分配给它时,body 会是什么样子?
附:我没有任何东西可以用来运行代码,所以我可以调试它,但我只是希望这对于了解 Python 的人来说足够简单,并且他们能够告诉我那条线上发生了什么代码。
【问题讨论】:
-
你能成功吗?我遵循了相同的步骤,但根本无法获得经过身份验证的 API 工作,得到 {"The remote server returned an error: (417) Expectation Failed."}
标签: python httprequest