【问题标题】:curl with file upload to python requestcurl 将文件上传到 python 请求
【发布时间】:2021-12-27 06:42:37
【问题描述】:

我正在尝试将以下 curl 转换为 python 请求。此请求会上传一个 zip 文件。

curl -k -i -X POST --form 'session.id=e7a29776-5783-49d7-afa0-b0e688096b5e' --form 'ajax=upload' --form 'file=@myproject.zip;type=application/zip' --form 'project=MyProject' https://localhost:8443/manager

使用 curl 到 python 转换工具 - 我明白了

import requests

files = {
    'session.id': (None, 'e7a29776-5783-49d7-afa0-b0e688096b5e'),
    'ajax': (None, 'upload'),
    'file': ('myproject.zip;type', open('myproject.zip;type', 'rb')),
    'project': (None, 'MyProject'),
}

response = requests.post('https://localhost:8443/manager', files=files, verify=False)

但这不起作用

【问题讨论】:

    标签: python python-3.x linux curl


    【解决方案1】:

    尝试分离数据和文件:

    import requests
    
    data = {
        "ajax": "upload",
        "project": "MyProject",
        "session.id": "e7a29776-5783-49d7-afa0-b0e688096b5e",
    }
    
    files = {"file": ("myproject.zip", open("myproject.zip", "rb"), "application/zip")}
    
    response = requests.post("https://localhost:8443/manager", data=data, verify=False, files=files)
    

    【讨论】:

    • 工作就像一个魅力!谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-09-02
    • 1970-01-01
    • 2017-11-02
    • 1970-01-01
    • 1970-01-01
    • 2017-10-11
    • 2019-05-20
    相关资源
    最近更新 更多