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

python中添加header头无法上传 #403

Closed
bpa6 opened this issue Apr 28, 2022 · 8 comments
Closed

python中添加header头无法上传 #403

bpa6 opened this issue Apr 28, 2022 · 8 comments

Comments

@bpa6
Copy link

bpa6 commented Apr 28, 2022

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)
@wisp-x
Copy link
Collaborator

wisp-x commented Apr 29, 2022

import requests

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

@bpa6
Copy link
Author

bpa6 commented Apr 29, 2022

导入 请求

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":{}}

@wisp-x
Copy link
Collaborator

wisp-x commented Apr 29, 2022

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

@bpa6
Copy link
Author

bpa6 commented Apr 29, 2022

laravel-2022-04-29.log

@wisp-x
Copy link
Collaborator

wisp-x commented Apr 29, 2022

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

@bpa6
Copy link
Author

bpa6 commented Apr 29, 2022

删除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"}}}

@txuw
Copy link

txuw commented Jun 5, 2022

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的构造出的请求有问题吗

@wisp-x
Copy link
Collaborator

wisp-x commented Jun 6, 2022

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

@wisp-x wisp-x closed this as completed Aug 17, 2022
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

3 participants