【问题标题】:Upload image through form data using python requests使用python请求通过表单数据上传图片
【发布时间】:2017-08-25 12:51:50
【问题描述】:

我正在尝试自动上传图片。

当我在浏览器中上传图片并查看网络选项卡时,我在请求正文中看到以下内容:

------WebKitFormBoundary053SrPeDVvrnxY3c
Content-Disposition: form-data; name="uploadId"

0:0:48deedc5937d0:1009c3
------WebKitFormBoundary053SrPeDVvrnxY3g
Content-Disposition: form-data; name="mtype"

1000
------WebKitFormBoundary053SrPeDVvrnxY3g
Content-Disposition: form-data; name="extensions"

png,gif
------WebKitFormBoundary053SrPeDVvrnxY3g
Content-Disposition: form-data; name="minPixSize"

1000
------WebKitFormBoundary053SrPeDVvrnxY3g
Content-Disposition: form-data; name="maxBytesSize"

1000
------WebKitFormBoundary053SrPeDVvrnxY3g
Content-Disposition: form-data; name="masterSize"


------WebKitFormBoundary053SrPeDVvrnxY3g
Content-Disposition: form-data; name="masterWidth"


------WebKitFormBoundary053SrPeDVvrnxY3g
Content-Disposition: form-data; name="masterHeight"


------WebKitFormBoundary053SrPeDVvrnxY3g
Content-Disposition: form-data; name="imageFile1"; filename="01.jpg"
Content-Type: image/jpeg


------WebKitFormBoundary053SrPeDVvrnxY3g--

如何使用 python requests lib 重复这样的请求?

问题是最后一部分:

------WebKitFormBoundary053SrPeDVvrnxY3g
Content-Disposition: form-data; name="imageFile1"; filename="01.jpg"
Content-Type: image/jpeg

所有其他字段都可以添加为传递给data参数的dict中的字段。

到目前为止,我尝试过这个:

requests.post(
    url="http://myserver.com/upload",
    headers={
        "Content-Type": "multipart/form-data",
    },
    data={
        "uploadId": "0:0:48deedc5937d0:1009c3",
        "mtype": "1000",
        "extensions": "png,gif",
        "minPixSize": "1000",
        "maxBytesSize": "1000",
        "masterSize": "",
        "masterWidth": "",
        "masterHeight": ""
    },
    files={'media': open("01.jpg", 'rb')}
)

服务器回复:

Could not parse multipart servlet request; nested exception is org.apache.commons.fileupload.FileUploadException: the request was rejected because no multipart boundary was found; id: null

【问题讨论】:

    标签: python file-upload request multipartform-data


    【解决方案1】:

    这行得通:

    import requests
    from requests_toolbelt.multipart.encoder import MultipartEncoder
    
    multipart_data = MultipartEncoder(
        fields={
            "uploadId": "0:2d7765623034:557915d737b48:000456",
            "mtype": "1000",
            "extensions": "png,gif",
            "minPixSize": "1000",
            "maxBytesSize": "1000",
            "masterSize": "",
            "masterWidth": "",
            "masterHeight": "",
            "imageFile1": (
                "filename.jpg",
                open("filename.jpg"], 'rb'),
                "image/jpeg"
            )
        }
    )
    
    requests.post(
        url="http://myserver.com/upload",
        headers={
            "Content-Type": multipart_data.content_type,
        },
        data=multipart_data,
    )
    

    【讨论】:

      猜你喜欢
      • 2015-05-20
      • 1970-01-01
      • 1970-01-01
      • 2016-03-28
      • 1970-01-01
      • 1970-01-01
      • 2013-05-01
      • 1970-01-01
      • 2019-03-21
      相关资源
      最近更新 更多