Как вы сказали в комментарии, вы не можете использовать запросы (это довольно грустно слышать!), Поэтому я сделал еще один фрагмент, используя urllib (краткий ответ: вы должны .encode('utf-8')
json.dumps
и decode('utf-8')
response.read()
):
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'))
Я попытался добавить несколько комментариев.Я надеюсь, что это решение поможет вам!
Чтобы помочь вам лучше изучить Python, вам следует прочитать Руководство по стилю для кода Python