python - 如何向 Intersango API 发出请求

标签 python httprequest

我正在尝试找出 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/

相关文章:

python - 错误的代码或奇怪的数学行为? ( python 3)

python - SQLite:仅返回每个组中的前 2 个结果

python - 如何在 python 中使用 action=fetch 向 url 发送 HTTP POST 请求?

java - 无法在 Volley 中模仿 POSTMAN 请求

python - 使用 Python 的动态表

python - 有没有办法在没有 scipy 其余部分的情况下安装 scipy 特殊模块?

python - 如何在一个图中绘制多个 seasonal_decompose 图?

java - 如何在下一个 Activity 中显示响应

c# - 如何确定 .NET 中的并发 HTTP 请求瓶颈?

c# - 使用UriBuilder并构造httpRequest