我正在尝试找出 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、逗号分隔的东西,或者它到底是如何序列化的?当参数被编码并分配给它时,主体会是什么样子?
附注我没有任何可以运行代码的东西,所以我可以调试它,但我只是希望这对于了解 Python 的人来说足够简单,可以理解,并且他们能够告诉我该行发生了什么代码。
最佳答案
params
是 2 元素列表的列表。该列表看起来像 ((key1, value1), (key2, value2), ...)
params.append(('api_key',self.api_key))
将另一个 2 元素列表添加到现有参数列表中。
最后,urllib.urlencode
获取此列表并将其转换为属性 urlencoded 字符串。在这种情况下,它将返回一个字符串 key1=value1&key2=value2&api_key=23423
。如果您的键或值中有任何特殊字符,urlencode 将对它们进行%编码。请参阅documentation for urlencode
关于python - 如何向 Intersango API 发出请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10291186/