python post request throws UnicodeEncodeError: 'ascii' codec can't encode character u'\xa0'
python post request throws UnicodeEncodeError: 'ascii' codec can't encode character u'\xa0'
我有 python 代码使用 REST API 从 Confluence 读取页面数据,然后使用该数据在 Confluence 中创建一个新页面。在 posting 数据时,代码抛出以下错误:
UnicodeEncodeError: 'ascii' codec can't encode character u'\xa0' in position 48108: ordinal not in range(128)
但是,它在 Postman 中工作得很好。我的猜测是 postman 处理编码。我发现许多建议使用 str.encode('utf-8')
和 str.encode('ascii', 'ignore')
的答案,但 none 对我有用。我也试过:
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
但徒劳无功。如果我使用 中的任何一个进行编码,那么在触发 post 请求时会出现 500 内部错误。
这是我的 python 代码:get_template_data
获取汇合页面详细信息并在 post 请求中使用它来创建新页面。
#5. Get the weekly status template page data
def get_template_data():
url = 'https://confluence.abc.com/rest/api/content/11111111?expand=body.export_view'
headers['content-type'] = "application/json"
r = requests.get(url, headers=headers)
data = r.json()
template_data=data['body']['export_view']['value']
return template_data
#6. Using weekly status template page(5) & string(2), create (post) a new page in Confluence.
def create_weekly_status_page(title,template_data):
post_data_prefix = """{"type":"page","title":"%s", "ancestors":[{"id":222222}], "space":{"key":"TST"},"body":{"storage":{"value":"%s","representation":"storage"}}}"""
#template_data1 = u' '.join(template_data).encode('utf-8')
template_data = template_data.replace('"', '\"')
post_body_str = post_data_prefix % (title, template_data)
url = 'https://confluence.abc.com/rest/api/content/'
headers['content-type'] = "application/json"
r = requests.post(url, headers=headers, data=post_body_str)
print r.status_code
r.raise_for_status()
def main():
#5. Get the weekly status template page data
template_data = get_template_data()
#print "template_data:", template_data
#6. Using weekly status template page(5) & string(2), create (post) a new page in Confluence.
# The weekly status string (2) will serve as title of the page
weekly_status_str = "Weekly Report (01/01/17 - 01/05/17)
create_weekly_status_page(weekly_status_str,template_data)
罪魁祸首是 r = requests.post(url, headers=headers, data=post_body_str)
。所以很可能 post_body_str
中的数据与此有关。
下面是 stracktrace:
Traceback (most recent call last):
File "new_status.py", line 216, in <module>
main()
File "new_status.py", line 193, in main
create_weekly_status_page(weekly_status_str,template_data)
File "new_status.py", line 79, in create_weekly_status_page
url = 'https://confluence.abc.com/rest/api/content/'
File "/Library/Python/2.7/site-packages/requests/api.py", line 110, in post
return request('post', url, data=data, json=json, **kwargs)
File "/Library/Python/2.7/site-packages/requests/api.py", line 56, in request
return session.request(method=method, url=url, **kwargs)
File "/Library/Python/2.7/site-packages/requests/sessions.py", line 488, in request
resp = self.send(prep, **send_kwargs)
File "/Library/Python/2.7/site-packages/requests/sessions.py", line 609, in send
r = adapter.send(request, **kwargs)
File "/Library/Python/2.7/site-packages/requests/adapters.py", line 423, in send
timeout=timeout
File "/Library/Python/2.7/site-packages/requests/packages/urllib3/connectionpool.py", line 594, in urlopen
chunked=chunked)
File "/Library/Python/2.7/site-packages/requests/packages/urllib3/connectionpool.py", line 361, in _make_request
conn.request(method, url, **httplib_request_kw)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 1053, in request
self._send_request(method, url, body, headers)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 1093, in _send_request
self.endheaders(body)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 1049, in endheaders
self._send_output(message_body)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 897, in _send_output
self.send(message_body)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 869, in send
self.sock.sendall(data)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ssl.py", line 721, in sendall
v = self.send(data[count:])
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ssl.py", line 687, in send
v = self._sslobj.write(data)
UnicodeEncodeError: 'ascii' codec can't encode character u'\xa0' in position 52926: ordinal not in range(128)
此外,我在获取模板数据时使用 body.export_view
,因为该页面包含宏,我不希望复制宏,而是复制宏的结果。因此使用 body.export_view
。
我对 Python 很陌生。并编写此代码以自动执行一些操作并同时学习 Python。会欣赏一些 help/pointers.
Python版本:2.7.10
在这两行的帮助下,我终于设法让它工作了:
template_data = template_data.encode('ascii', 'ignore')
和
template_data = template_data.replace('\n', '\n')
第一行修正了 UnicodeEncodeError
而第二行修正了 500 Internal Error
.
响应 = requests.get(url = url, headers = headers)
我的数据 = response.text.encode('ascii', 'ignore')
在浪费了一天时间后,上面这行为我解决了这个问题 :( :(
我有 python 代码使用 REST API 从 Confluence 读取页面数据,然后使用该数据在 Confluence 中创建一个新页面。在 posting 数据时,代码抛出以下错误:
UnicodeEncodeError: 'ascii' codec can't encode character u'\xa0' in position 48108: ordinal not in range(128)
但是,它在 Postman 中工作得很好。我的猜测是 postman 处理编码。我发现许多建议使用 str.encode('utf-8')
和 str.encode('ascii', 'ignore')
的答案,但 none 对我有用。我也试过:
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
但徒劳无功。如果我使用 中的任何一个进行编码,那么在触发 post 请求时会出现 500 内部错误。
这是我的 python 代码:get_template_data
获取汇合页面详细信息并在 post 请求中使用它来创建新页面。
#5. Get the weekly status template page data
def get_template_data():
url = 'https://confluence.abc.com/rest/api/content/11111111?expand=body.export_view'
headers['content-type'] = "application/json"
r = requests.get(url, headers=headers)
data = r.json()
template_data=data['body']['export_view']['value']
return template_data
#6. Using weekly status template page(5) & string(2), create (post) a new page in Confluence.
def create_weekly_status_page(title,template_data):
post_data_prefix = """{"type":"page","title":"%s", "ancestors":[{"id":222222}], "space":{"key":"TST"},"body":{"storage":{"value":"%s","representation":"storage"}}}"""
#template_data1 = u' '.join(template_data).encode('utf-8')
template_data = template_data.replace('"', '\"')
post_body_str = post_data_prefix % (title, template_data)
url = 'https://confluence.abc.com/rest/api/content/'
headers['content-type'] = "application/json"
r = requests.post(url, headers=headers, data=post_body_str)
print r.status_code
r.raise_for_status()
def main():
#5. Get the weekly status template page data
template_data = get_template_data()
#print "template_data:", template_data
#6. Using weekly status template page(5) & string(2), create (post) a new page in Confluence.
# The weekly status string (2) will serve as title of the page
weekly_status_str = "Weekly Report (01/01/17 - 01/05/17)
create_weekly_status_page(weekly_status_str,template_data)
罪魁祸首是 r = requests.post(url, headers=headers, data=post_body_str)
。所以很可能 post_body_str
中的数据与此有关。
下面是 stracktrace:
Traceback (most recent call last):
File "new_status.py", line 216, in <module>
main()
File "new_status.py", line 193, in main
create_weekly_status_page(weekly_status_str,template_data)
File "new_status.py", line 79, in create_weekly_status_page
url = 'https://confluence.abc.com/rest/api/content/'
File "/Library/Python/2.7/site-packages/requests/api.py", line 110, in post
return request('post', url, data=data, json=json, **kwargs)
File "/Library/Python/2.7/site-packages/requests/api.py", line 56, in request
return session.request(method=method, url=url, **kwargs)
File "/Library/Python/2.7/site-packages/requests/sessions.py", line 488, in request
resp = self.send(prep, **send_kwargs)
File "/Library/Python/2.7/site-packages/requests/sessions.py", line 609, in send
r = adapter.send(request, **kwargs)
File "/Library/Python/2.7/site-packages/requests/adapters.py", line 423, in send
timeout=timeout
File "/Library/Python/2.7/site-packages/requests/packages/urllib3/connectionpool.py", line 594, in urlopen
chunked=chunked)
File "/Library/Python/2.7/site-packages/requests/packages/urllib3/connectionpool.py", line 361, in _make_request
conn.request(method, url, **httplib_request_kw)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 1053, in request
self._send_request(method, url, body, headers)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 1093, in _send_request
self.endheaders(body)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 1049, in endheaders
self._send_output(message_body)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 897, in _send_output
self.send(message_body)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 869, in send
self.sock.sendall(data)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ssl.py", line 721, in sendall
v = self.send(data[count:])
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ssl.py", line 687, in send
v = self._sslobj.write(data)
UnicodeEncodeError: 'ascii' codec can't encode character u'\xa0' in position 52926: ordinal not in range(128)
此外,我在获取模板数据时使用 body.export_view
,因为该页面包含宏,我不希望复制宏,而是复制宏的结果。因此使用 body.export_view
。
我对 Python 很陌生。并编写此代码以自动执行一些操作并同时学习 Python。会欣赏一些 help/pointers.
Python版本:2.7.10
在这两行的帮助下,我终于设法让它工作了:
template_data = template_data.encode('ascii', 'ignore')
和
template_data = template_data.replace('\n', '\n')
第一行修正了 UnicodeEncodeError
而第二行修正了 500 Internal Error
.
响应 = requests.get(url = url, headers = headers)
我的数据 = response.text.encode('ascii', 'ignore')
在浪费了一天时间后,上面这行为我解决了这个问题 :( :(