【发布时间】:2014-07-22 08:18:04
【问题描述】:
我想向具有特定数据和标头类型的 URL 发送发布请求。使用这两个链接我发现了如何做到这一点,但它不起作用:
https://stackoverflow.com/questions/5693931/python-post-request
How do I send a custom header with urllib2 in a HTTP Request?
这是我的代码:
import urllib
import urllib2
url = 'https://clients6.google.com/rpc'
values = [
{"method": "pos.plusones.get",
"id": "p",
"params": {
"nolog": True,
"id": "http://www.newswhip.com",
"source": "widget",
"userId": "@viewer",
"groupId": "@self"
},
"jsonrpc": "2.0",
"key": "p",
"apiVersion": "v1"
}]
headers = {"Content-type" : "application/json:"}
data = urllib.urlencode(values)
req = urllib2.Request(url, data, headers)
response = urllib2.urlopen(req)
the_page = response.read()
print the_page
我可以在 Postman Rest Client 中使用这些值获得结果。但执行此代码后,结果是:
Traceback (most recent call last):
File "D:/Developer Center/Republishan/republishan2/republishan2/test2.py", line 22, in <module>
data = urllib.urlencode(values)
File "C:\Python27\lib\urllib.py", line 1312, in urlencode
raise TypeError
TypeError: not a valid non-string sequence or mapping object
我也尝试过使用字典而不是这样的列表:
values = {"method": "pos.plusones.get",
"id": "p",
"params": {
"nolog": True,
"id": "http://www.newswhip.com",
"source": "widget",
"userId": "@viewer",
"groupId": "@self"
},
"jsonrpc": "2.0",
"key": "p",
"apiVersion": "v1"
}
它执行脚本但结果包含错误:
{"error":{"code":-32700,"message":"Unable to parse json","data":[{"domain":"global","reason":"parseError","message":"Unable to parse json"}]}}
正如我所说,我可以使用 Postman Rest Client 使用列表而不是字典来执行脚本。 在 Postman 中查看结果: 我该怎么办?
【问题讨论】:
-
values应该是字典而不是列表。试试values = {"method": "pos.plusones.get", ...., "apiVersion": "v1"} -
问题已更新,它工作但结果是一个错误。
-
一些服务器错误,我想。检查您发送的值的格式是否正确,并且服务器以相同的方式期望它。
-
这些值正在与 Rest 客户端一起使用!我认为需要在 urllib 中进行额外配置。
-
@sk11 看我问题中的图片