lsky-pro icon indicating copy to clipboard operation
lsky-pro copied to clipboard

python中添加header头无法上传

Open bpa6 opened this issue 3 years ago • 8 comments

import requests

header = {
    "Content-Type": "multipart/form-data"
}

files = [
    ('file', open('tu.jpg', 'rb'))
]
url = 'https://url/api/v1/upload'
#a1 = requests.post(url=url, files=files, headers=header, verify=False)
a1 = requests.post(url=url, files=files, verify=False)
print(a1.text)

bpa6 avatar Apr 28 '22 17:04 bpa6

import requests

headers = {
  'Content-Type": "multipart/form-data',
  'Accept': 'application/json',
  'x-requested-with': 'XMLHttpRequest',
}


0xxb avatar Apr 29 '22 02:04 0xxb

导入 请求

headers  = {
   'Content-Type': "multipart/form-data' ,
   'Accept' : 'application/json' ,
   'x-requested-with' : 'XMLHttpRequest' ,
}

{"status":false,"message":"\u670d\u52a1\u5f02\u5e38\uff0c\u8bf7\u7a0d\u540e\u518d\u8bd5","data":{}}

bpa6 avatar Apr 29 '22 03:04 bpa6

前往文件夹 storage/logs 目录,里面有详细的错误日志。

0xxb avatar Apr 29 '22 03:04 0xxb

看起来是没有获取到文件拓展名称,检查一下你这个文件上传的对不对吧,python 我不太懂。

0xxb avatar Apr 29 '22 03:04 0xxb

删除header头可以上传 {"status":true,"message":"\u4e0a\u4f20\u6210\u529f","data":{"key":"gm2pc5","name":"626ac1ac54129.jpg","pathname":"2022\/04\/29\/626ac1ac54129.jpg","origin_name":"no.jpg","size":5.095703125,"mimetype":"image\/jpeg","extension":"jpg","md5":"c080d2ec112dcd89c3f45e243381c6e3","sha1":"e1432b68297d3faed808ebac7007556596d9a3be","links":{"url":"https:\/\/test.com\/i\/2022\/04\/29\/626ac1ac54129.jpg","html":"<img src=\"https:\/\/test.com\/i\/2022\/04\/29\/626ac1ac54129.jpg\" alt=\"no.jpg\" title=\"no.jpg\" \/>","bbcode":"[img]https:\/\/test.com\/i\/2022\/04\/29\/626ac1ac54129.jpg[\/img]","markdown":"![no.jpg](https:\/\/test.com\/i\/2022\/04\/29\/626ac1ac54129.jpg)","markdown_with_link":"[![no.jpg](https:\/\/test.com\/i\/2022\/04\/29\/626ac1ac54129.jpg)](https:\/\/test.com\/i\/2022\/04\/29\/626ac1ac54129.jpg)","thumbnail_url":"https:\/\/test.com\/thumbnails\/c080d2ec112dcd89c3f45e243381c6e3.png"}}}

bpa6 avatar Apr 29 '22 03:04 bpa6

import requests
headers = {
    # "Content-Type":"multipart/form-data",
    "Authorization":"xxx",
    "Accept":"application/json",
}


if __name__ == "__main__":
    path = 'F:/Music/ccc.png'
    data = {
    "file" :('file.jpg',open(path,'rb'),'image/png')
    }
    # files = [
    #     ('file',open(path,'rb'))
    # ]
    # print(headers)
    url = 'xxx'
    r = requests.post(url,files = data,headers=headers)
    r.encoding = "utf-8"
    print(r.json())

将content-type注释以后也可以正常上传,并以token账户传输,但识别成form-data后无法正常读取,不太理解是为什么, @wisp-x ,是python的构造出的请求有问题吗

txuw avatar Jun 05 '22 06:06 txuw

@txuw 抱歉,我对 python 不是很了解。

0xxb avatar Jun 06 '22 08:06 0xxb