Unicode错误 - 带文件对象的POST请求

时间:2015-09-17 08:25:17

标签: django python-2.7 django-rest-framework python-unicode

我有一个名为testing-unicode-ö.png的文件。我想创建一个文件对象,并将其发送到我的API,在那里将读取内容并格式化文件对象名称。

此代码适合我的django-rest-framework测试中的APITestCase子类。

# -*- coding: utf-8 -*-
from __future__ import unicode_literals

fname = os.path.join(settings.BASE_DIR, "./testing-unicode-ö.png"))
f = open(fname.encode('utf-8'))

payload = {"file": f}
headers = {"HTTP_X_SECRET_KEY": "some-key"}
url = "some-url"

response = self.client.post(url, data=payload, **headers)

我在尝试UnicodeDecodeError时收到self.client.post

----------------------------------------------------------------------
Traceback (most recent call last):
  File "/code/sapi/widgets/tests/test_api.py", line 1517, in test_image_upload_no_unicode_error
    response = self.client.post(url, data=payload, **headers)
  File "/usr/local/lib/python2.7/dist-packages/rest_framework/test.py", line 170, in post
    path, data=data, format=format, content_type=content_type, **extra)
  File "/usr/local/lib/python2.7/dist-packages/rest_framework/test.py", line 91, in post
    data, content_type = self._encode_data(data, format, content_type)
  File "/usr/local/lib/python2.7/dist-packages/rest_framework/test.py", line 66, in _encode_data
    ret = renderer.render(data)
  File "/usr/local/lib/python2.7/dist-packages/rest_framework/renderers.py", line 782, in render
    return encode_multipart(self.BOUNDARY, data)
  File "/usr/local/lib/python2.7/dist-packages/django/test/client.py", line 170, in encode_multipart
    lines.extend(encode_file(boundary, key, value))
  File "/usr/local/lib/python2.7/dist-packages/django/test/client.py", line 214, in encode_file
    % (key, filename)),
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 16: ordinal not in range(128)

它在client.py - django中的 Ubuntu 15.04, python 2.7.3, django 1.8.2, django-rest-framework 3.2.2 爆炸了。难住了。

我的设置是:Sub Shiftup() Dim rng As Range Dim inp As Range Set inp = Selection On Error Resume Next Set rng = Application.InputBox("Range to Shift Up", Type:=8) On Error GoTo 0 If TypeName(rng) <> "Range" Then Exit Sub Else rng.Select rng.Rows.Delete Shift:=xlShiftUp End If End Sub

0 个答案:

没有答案