UnicodeEncodeError:'latin-1'编解码器无法在位置2740处编码字符'\ u201c':序数不在范围内(256)

时间:2019-03-11 00:39:33

标签: xml python-3.x rest encode python-unicode

我收到UnicodeEncodeError:'latin-1'编解码器无法在位置2740处对字符'\ u201c'进行编码:当尝试使用以下代码进行REST POST时,序数不在range(256)中:

import requests, sys, base64, collections, json, urllib3, re
from getpass import getpass
from requests.auth import HTTPBasicAuth
from xml.etree import ElementTree

# disable TLS warnings
urllib3.disable_warnings()

print ("\nPlease enter your 1DC username:")
user = input()
print ("\n")
password = getpass("Please enter your 1DC password:")
print ("\n")

xml = """<?xml version="1.0" encoding="utf-8"?>
<ticket>
  <id>#####</id>
  <subject>Test Firewall Request For API</subject>
  <requester>###</requester>
</ticket>"""

header={'Authorization':'HTTPBasicAuth(user, password)','Content- 
Type':'application/xml'}

response=requests.post('https://server_ip/securechangeworkflow/api/securechange/tickets/', data=xml, headers=header, verify=False)
print (response.text.encode("utf-8"))


Traceback (most recent call last):
  File "api_tufin_7.py", line 313, in <module>
response=requests.post('https://server_ip/securechangeworkflow/api/securechange/tickets/', data=xml, headers=header, verify=False)

File "C:\Program Files\Python35\lib\site-packages\requests\api.py", line 116, in post
return request('post', url, data=data, json=json, **kwargs)
File "C:\Program Files\Python35\lib\site-packages\requests\api.py", line 60, in request
return session.request(method=method, url=url, **kwargs)
File "C:\Program Files\Python35\lib\site-packages\requests\sessions.py", line 533, in request
resp = self.send(prep, **send_kwargs)
File "C:\Program Files\Python35\lib\site-packages\requests\sessions.py", line 646, in send
r = adapter.send(request, **kwargs)
File "C:\Program Files\Python35\lib\site-packages\requests\adapters.py", line 449, in send
timeout=timeout
File "C:\Program Files\Python35\lib\site-packages\urllib3\connectionpool.py", line 600, in urlopen
chunked=chunked)
File "C:\Program Files\Python35\lib\site-packages\urllib3\connectionpool.py", line 354, in _make_request
conn.request(method, url, **httplib_request_kw)
File "C:\Program Files\Python35\lib\http\client.py", line 1083, in request
self._send_request(method, url, body, headers)
File "C:\Program Files\Python35\lib\http\client.py", line 1127, in _send_request
body = body.encode('iso-8859-1')
UnicodeEncodeError: 'latin-1' codec can't encode character '\u201c' in position 2740: ordinal not in range(256)

我不确定是什么原因导致此错误。

0 个答案:

没有答案