question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Posting a mutlipart-encoded file with non-ASCII characters in filename doesn't work

See original GitHub issue

The following code doesn’t actually upload any data to the server:

r = requests.post('https://gs.smuglo.li/api/statusnet/media/upload',
    auth=('testbot', 'testbot'),
    files={'media': open('/tmp/Снимок экрана_2016-07-27_05-15-38.png', 'rb')})

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:14 (7 by maintainers)

github_iconTop GitHub Comments

3reactions
ghostcommented, Jul 27, 2016

@Lukasa Is this Python 2? Because in Python 3 I get

TypeError: a bytes-like object is required, not 'str'
2reactions
ghostcommented, Jul 27, 2016

@Lukasa Sure

>>> filename = '/tmp/Снимок экрана_2016-07-27_05-15-38.png' 
>>> media = {'media':(filename.encode('utf-8'), open(filename, 'rb'))}
>>> r = requests.post(url, auth=('testbot', 'testbot'), files=media)
Traceback (most recent call last):
  File "<input>", line 1, in <module>
    r = requests.post(url, auth=(username, password), files=media)
  File "/usr/lib/python3.5/site-packages/requests/api.py", line 111, in post
    return request('post', url, data=data, json=json, **kwargs)
  File "/usr/lib/python3.5/site-packages/requests/api.py", line 57, in request
    return session.request(method=method, url=url, **kwargs)
  File "/usr/lib/python3.5/site-packages/requests/sessions.py", line 461, in request
    prep = self.prepare_request(req)
  File "/usr/lib/python3.5/site-packages/requests/sessions.py", line 394, in prepare_request
    hooks=merge_hooks(request.hooks, self.hooks),
  File "/usr/lib/python3.5/site-packages/requests/models.py", line 298, in prepare
    self.prepare_body(data, files, json)
  File "/usr/lib/python3.5/site-packages/requests/models.py", line 449, in prepare_body
    (body, content_type) = self._encode_files(files, data)
  File "/usr/lib/python3.5/site-packages/requests/models.py", line 155, in _encode_files
    rf.make_multipart(content_type=ft)
  File "/usr/lib/python3.5/site-packages/urllib3/fields.py", line 174, in make_multipart
    (('name', self._name), ('filename', self._filename))
  File "/usr/lib/python3.5/site-packages/urllib3/fields.py", line 134, in _render_parts
    parts.append(self._render_part(name, value))
  File "/usr/lib/python3.5/site-packages/urllib3/fields.py", line 114, in _render_part
    return format_header_param(name, value)
  File "/usr/lib/python3.5/site-packages/urllib3/fields.py", line 35, in format_header_param
    if not any(ch in value for ch in '"\\\r\n'):
  File "/usr/lib/python3.5/site-packages/urllib3/fields.py", line 35, in <genexpr>
    if not any(ch in value for ch in '"\\\r\n'):
TypeError: a bytes-like object is required, not 'str'
Read more comments on GitHub >

github_iconTop Results From Across the Web

Posting a mutlipart-encoded file with non-ASCII characters in ...
The following code doesn't actually upload any data to the server: r ... file with non-ASCII characters in filename doesn't work #3446.
Read more >
Non-ASCII URLs and Filenames - Tiger Technologies Support
Our servers let you use filenames that contain non-ASCII characters. For example, you could name a file with an accented letter “e”: tést.html....
Read more >
c# - double encoded filename in ContentDisposition non-ascii ...
I had the same problem when try to get the FileName from files with name with special chars, in my case using accents....
Read more >
Non-ASCII chars in filename - Power Platform Community
Solved: Hi, I'm trying to build a flow that sends an email when a certain file is modified in Sharepoint. I have used...
Read more >
identify files with non-ASCII or non-printable characters in file ...
Other than trawling through the file list laboriously is there: An easy way to list or otherwise identify these file names? A way...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found