<p>正如您在评论中所说的,您不能使用请求(听到这个消息很难过!),所以我用urllib做了另一个片段(简短的回答:您必须<code>.encode('utf-8')</code><code>json.dumps</code>和<code>decode('utf-8')</code><code>response.read()</code>):</p>
<pre><code>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'))
</code></pre>
<p>我确实试着添加一些评论。我希望这个解决方案对你有帮助!你知道吗</p>
<p>为了帮助您更好地学习python,您应该阅读<a href="https://pep8.org/" rel="nofollow noreferrer">Style Guide for Python Code</a></p>