Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to post file that filename include chinese name #4075

Closed
zming opened this issue Sep 18, 2019 · 5 comments
Closed

How to post file that filename include chinese name #4075

zming opened this issue Sep 18, 2019 · 5 comments

Comments

@zming
Copy link

zming commented Sep 18, 2019

Long story short

I want to post a file to jira api, but not get a chinese name response

How to post file that filename include chinese name

Expected behaviour

{...
"filename":"中文.md",
...}

Actual behaviour

{...
"filename":"%E6%9C%AA%E5%91%BD%E5%90%8D.md",
...}

Steps to reproduce

from urllib import parse
import base64
import aiohttp
import asyncio

from aiohttp import FormData

def base64_encode(string):
    encode_string = base64.b64encode(string.encode('utf-8'))
    return str(encode_string,"utf-8")

async def main():
    url = 'http://193.112.46.206:8080/rest/api/2/issue/JIRA-1/attachments'

    data = FormData(charset="UTF-8")
    data.add_field('file',
                   open('中文.md', 'rb'),
                   filename='中文.md',
                   content_type='application/md')
    token = "Basic " + base64_encode("admin:123456")
    headers = {'X-Atlassian-Token': 'no-check',"Authorization":token}
    print(url)
    async with aiohttp.ClientSession() as session:
        async with session.post(url, data=data,headers=headers,) as resp:
            print(resp.status)
            print(await resp.text(encoding="utf-8"))


loop = asyncio.get_event_loop()
loop.run_until_complete(main())

I got the response, the filename incorrect

[{...
"filename":"%E4%B8%AD%E6%96%87.md"
...}]

if I use curl command, I got the Expected result

curl -D- -u admin:123456 -X POST -H "X-Atlassian-Token: no-check" -F "file=@中文.md" http://193.112.46.206:8080/rest/api/2/issue/JIRA-1/attachments
HTTP/1.1 100 

HTTP/1.1 200 
X-AREQUESTID: 1019x735x1
X-XSS-Protection: 1; mode=block
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
Content-Security-Policy: frame-ancestors 'self'
X-ASEN: SEN-L14209471
Set-Cookie: JSESSIONID=4127D49BB16DD99A669CA18B972EE711; Path=/; HttpOnly
X-Seraph-LoginReason: OK
Set-Cookie: atlassian.xsrf.token=BJ1H-386Y-A5GZ-A6XR_e09ef403bffb8cbc3bbcddf074859ac0e419d23f_lin; Path=/
X-ASESSIONID: 1hj5kfz
X-AUSERNAME: admin
Cache-Control: no-cache, no-store, no-transform
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Date: Wed, 18 Sep 2019 16:59:13 GMT

[{..."filename":"中文.md",...}]

Your environment

python: 3.6.5
OS: windows 10

@mshandrovskiy
Copy link

The decoding looks correct.

>>> import urllib.parse
>>> foo='%E6%9C%AA%E5%91%BD%E5%90%8D.md'
>>> urllib.parse.unquote(foo)
'未命名.md'

@asvetlov
Copy link
Member

Is it related to #4012 ?

@zming
Copy link
Author

zming commented Sep 19, 2019

Thanks!
I found a useful way. set function FormData(quote_fields=False) ,like this

    data = FormData(quote_fields=False)
    filename = parse.unquote('中文.md')
    data.add_field('file',
                   open('中文.md', 'rb'),
                   content_type='application/md',
                   filename='中文.md'
                   )

It work

@kohtala
Copy link
Contributor

kohtala commented Oct 10, 2019

I think this is related to #4012, but with my patch #4031 the quote_fields=False will still be needed. With the default it'll still perform the file name encoding. What the patch #4031 would help is that quote_fields=False will escape any quotes in the file name so that the result will still parse correctly.

@Dreamsorcerer
Copy link
Member

The spec recommends using ASCII only, so the default behaviour here seems correct, with quote_fields available to disable it.

@Dreamsorcerer Dreamsorcerer closed this as not planned Won't fix, can't repro, duplicate, stale Aug 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants