V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
bestehen
V2EX  ›  问与答

Python 通过 multipart 协议发送 post 请求 报错如下

  •  
  •   bestehen · 2018-08-26 12:33:23 +08:00 · 1466 次点击
    这是一个创建于 2308 天前的主题,其中的信息可能已经有所发展或是发生改变。
    querystring = {"_": "1111111"}

    payload = "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"_csrf\"\r\n\r\n{}\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"ReportForm[url]\"; filename=\"{}\"\r\nContent-Type: application/pdf\r\n\r\n\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--".format(csrf_token,'/home/shenjianlin/test_pdf'+'/'+'麦肯锡中国银行业 CEO 季刊-2018 年冬季刊精简版-137 页.pdf')
    header= {
    'content-type': "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW",
    'User-Agent': "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36",
    'Cookie': "u=400; PHPSESSID=dfc4ac95c5c485a49ec167febc0b2da6; _csrf=0226ebc29a0710864db49376b6c400a3895ce0dbfb7161dc3d08e5bc8d148386a%3A2%3A%7Bi%3A0%3Bs%3A5%3A%22_csrf%22%3Bi%3A1%3Bs%3A32%3A%22aoSW7OAr9i2rwVb2H7Khty6qSimxBUMn%22%3B%7D",
    'Cache-Control': "no-cache",
    'Postman-Token': "64fd1e6e-e442-4161-b32e-110c6627ee23"
    }

    response = requests.request("POST", url, data=payload, headers=header, params=querystring)
    print(response.text)



    Traceback (most recent call last):
    File "upload_pdf.py", line 66, in <module>
    up_load_pdf()
    File "upload_pdf.py", line 40, in up_load_pdf
    response = requests.request("POST", url, data=payload, headers=header, params=querystring)
    File "/usr/lib/python3.4/site-packages/requests/api.py", line 58, in request
    return session.request(method=method, url=url, **kwargs)
    File "/usr/lib/python3.4/site-packages/requests/sessions.py", line 508, in request
    resp = self.send(prep, **send_kwargs)
    File "/usr/lib/python3.4/site-packages/requests/sessions.py", line 618, in send
    r = adapter.send(request, **kwargs)
    File "/usr/lib/python3.4/site-packages/requests/adapters.py", line 440, in send
    timeout=timeout
    File "/usr/lib/python3.4/site-packages/urllib3/connectionpool.py", line 601, in urlopen
    chunked=chunked)
    File "/usr/lib/python3.4/site-packages/urllib3/connectionpool.py", line 357, in _make_request
    conn.request(method, url, **httplib_request_kw)
    File "/usr/lib64/python3.4/http/client.py", line 1137, in request
    self._send_request(method, url, body, headers)
    File "/usr/lib64/python3.4/http/client.py", line 1181, in _send_request
    body = body.encode('iso-8859-1')
    UnicodeEncodeError: 'latin-1' codec can't encode characters in position 313-320: ordinal not in range(256)
    xubeiyan
        1
    xubeiyan  
       2018-08-26 15:33:57 +08:00 via Android
    latin-1 编码不能编码文件名,换成 utf-8 就是了
    bestehen
        2
    bestehen  
    OP
       2018-08-26 16:06:49 +08:00
    @xubeiyan 我换了 utf-8
    但是传的文件是空
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2679 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 21ms · UTC 14:28 · PVG 22:28 · LAX 06:28 · JFK 09:28
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.