TypeError:POST数据应为字节、字节的iterable或文件对象。在python中尝试传递PUT请求时,它不能是str类型
我正在尝试将字典(字符串)列表传递给一个put请求的。我得到这个错误: TypeError:POST数据应该是字节,一个字节的iterable 这是使用TypeError:POST数据应为字节、字节的iterable或文件对象。在python中尝试传递PUT请求时,它不能是str类型,python,urllib,put,Python,Urllib,Put,我正在尝试将字典(字符串)列表传递给一个put请求的。我得到这个错误: TypeError:POST数据应该是字节,一个字节的iterable 这是使用python中的字典(字符串)列表发出put请求的正确方法吗 列表如下所示: list1 = ['{"id" : "","email" : "John@fullcontact.com","fullName": "John Lorang"}', '{"id" : "","email" : "Lola@fullcontact.com","fullNa
python
中的字典(字符串)列表发出put请求的正确方法吗
列表如下所示:
list1 = ['{"id" : "","email" : "John@fullcontact.com","fullName": "John Lorang"}', '{"id" : "","email" : "Lola@fullcontact.com","fullName": "Lola Dsilva"}']
myData = json.dumps(list1)
myRestRequestObj = urllib.request.Request(url,myData)
myRestRequestObj.add_header('Content-Type','application/json')
myRestRequestObj.add_header('Authorization','Basic %s')
myRestRequestObj.get_method = lambda : 'PUT'
try:
myRestRequestResponse = urllib.request.urlopen(myRestRequestObj)
except urllib.error.URLError as e:
print(e.reason)
我假设您可以使用
请求
模块(pip安装请求
)
是一个简单但功能强大的HTTP libraby for Python
import json
import requests
my_data = json.dumps(list1)
headers = {
'Authorization': 'Basic {token}'.format(token=your_token)
}
response = requests.put(url, headers=headers, json=my_data)
print("Status code: {status}".format(status=response.status_code))
print("raw response: {raw_response}".format(
raw_response=response.text
)
print("json response: {json_response}".format(
json_response=response.json()
)
正如您在一篇评论中所说,您不能使用请求(听到这个消息很伤心!),因此我使用urllib做了另一个片段(简短的回答:您必须
.encode('utf-8')
json.dumps
和decode('utf-8')
响应.read()
):
我确实试着添加一些评论。我希望这个解决方案对你有帮助
为了帮助您更好地学习python,您应该阅读请帮助我。您能使用requests软件包吗?它可能会解决您的问题。实际上,我已经安装了它,因为我尝试这是为了我的正式工作P我需要我的客户对此的权限。但是我安装了urllib,urllib。这不可能吗?有可能,我会在几个小时内回答你。当然。提前谢谢!
import urllib.request
import urllib.error
import json
url = 'http://httpbin.org/put'
token = 'jwtToken'
list1 = ['{"id" : "","email" : "John@fullcontact.com","fullName": "John Lorang"}', '{"id" : "","email" : "Lola@fullcontact.com","fullName": "Lola Dsilva"}']
# Request needs bytes, so we have to encode it
params = json.dumps(list1).encode('utf-8')
headers = {
'Content-Type': 'application/json',
'Authorization': 'Basic {token}'.format(token=token)
}
# Let's try to create our request with data, headers and method
try:
request = urllib.request.Request(url, data=params, headers=headers, method='PUT')
except urllib.error.URLError as e:
# Unable to create our request, here the reason
print("Unable to create youro request: {error}".format(error=str(e)))
else:
# We did create our request, let's try to use it
try:
response = urllib.request.urlopen(request)
except urllib.error.HTTPError as e:
# An HTTP error occured, here the reason
print("HTTP Error: {error}".format(error=str(e)))
except Exception as e:
# We got another reason, here the reason
print("An error occured while trying to put {url}: {error}".format(
url=url,
error=str(e)
))
else:
# We are printing the result
# We must decode it because response.read() returns a bytes string
print(response.read().decode('utf-8'))