如何在python中以base64编码发布屏幕截图

时间:2019-07-20 02:48:29

标签: python post python-requests base64 boundary

我想在python中进行POST调用以发布图像(图像是base64字符串)。我是刚开始使用Border进行POST调用的人。预计POST调用将采用以下提到的格式。任何可以实现的想法都将得到赞赏

POST .../api/URL_PATH
Content-Type: multipart/form-data; boundary=someboundary
Accept: application/json

--someboundary
Content-Disposition: form-data; name="entity"
Content-Type: application/json; charset=UTF-8
{
                "item": {
                                "id": 1002345,
                                "type": "type"
                },
                "name": "screenshot.png",
                "description": "This is screenshot"
}
--someboundary
Content-Disposition: form-data; name="content"; filename="screenshot.png"
Content-Type: image/png
Content-Transfer-Encoding: base64

iVBORw0KGgoAAAANSUhEUgAAALAAAAAzCAIAAAC48RV/AAAAAXNSR0IArs4c6QAAAARnQU1BAACx
jwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAALRSURBVHhe7ZjhUewwDITph3roh3qoh36OOLYc
rSRHd445YNjv17Msr2R5J9y8lxshChqCADQEAWgIAtAQBKAhCEBDEICGIAANQQAaggA0BAFoCALQ
EASgIQhAQxCAhiDAXzTE5/vrS+H1/bNFyDJoCAJ8gyE+3oLnCoOT/FZDrLzjj0FDrOM/G0LepNFn
0GaiefsIgzV/pCPAvuwqQ6h9fbZWLBFVuyX4iAJblS4LrVKsObh43UKUTtx8Uqiijh6YnDkmDBE2
07oJ5zIY1plOIdivm/HBjf4EQcUB6tXCQ72dpOjgjp6hjlTKbzfKOEZ3gVlDHLeVUfR2WgDbC4KJ
TqRb/60GUo/3gKjJWZewkRzp5SSjJeRFRULf0eN0jmaN7qgQlpHd3sVFFvyGsHMI53LHsDClraKL
ygyUmJF31fwREwn6w1HnRSMNT6CDoaxQ2+/bdn2RSUO0FjW9o3Aug2ENdYKxdLKRRdVcBEVkFYEZ
J5q+akCg04vv1ksL4UrSf/AL0ftH+gXCuQTBU51gLJ10ZEE1F0GRQS87T/tC3GmIsNlVfpgwROvu
aOGuufjguQ4OCclHNqx2RIyIP2HIi+YahbFOC6WFJL1zXvBBLhtC+j/aMgkVH0x0vG6J7MnpyOxy
w0WMiC+3sQdNfyea5kY77lgvJHm2claoLXSRlUz8yZB7G44LYELr3AcznWh/F8vfxiw3XOTspYDW
f17Udrwf7KEmMyzjEkaFYgE5fZm5H5X65vL/DOoCej8clwQzHby87ORvY5YbLhKI+GGrMedFCy1U
qGF3TAVU8iOFbJOCOnCBOUOQaYL3fgg5rywkxlrylaAhnsxVQ/jPUv9i0BB/kVVfCMeavxg0xLO5
aoiC+vFRWfJtqNAQBKAhCEBDEICGIAANQQAaggA0BAFoCALQEASgIQhAQxCAhiAADUEAGoIANAQB
aAgC0BBEcbt9ATZhuQV8mjteAAAAAElFTkSuQmCC
--a1b2c3d4--

下面是我尝试的代码

url = 'URL'
files = {'file': ('screenshot.png', 'base64 string')}

response = requests.post(url, auth=('*****', '******'), data={"item" : { "id": 1002345, "type": "type"
                },
                "name": "screenshot.png",
                "description": "This is screenshot"
}, files=files)

1 个答案:

答案 0 :(得分:0)

我相信这就是您想要的。痛苦的要求组装!我包括了日志记录功能,因此您可以确切地看到结果。在json数据上设置内容类型的唯一方法是将其添加为文件,然后手动使用“无”文件名进行设置。

import requests
import base64
import logging
import json

# Setup logging of raw requests
try:
    import http.client as http_client
except ImportError:
    # Python 2
    import httplib as http_client
http_client.HTTPConnection.debuglevel = 1

logging.basicConfig()
logging.getLogger().setLevel(logging.DEBUG)
requests_log = logging.getLogger("requests.packages.urllib3")
requests_log.setLevel(logging.DEBUG)
requests_log.propagate = True


# Setup your file
subheaders = dict()
subheaders["Content-Transfer-Encoding"] = "base64"
encodedImage = base64.b64encode(open("screenshot.png", "rb").read())

# Setup separate json data
payload = {"item" : { "id": 1002345, "type": "type" },
           "name": "screenshot.png",
           "description": "This is screenshot"}

# Build Files
files = { 'entity': (None, json.dumps(payload), 'application/json; charset=UTF-8'),
          'content': ("screenshot.png", encodedImage, "image/png", subheaders) }

# POST!
url = "http://localhost:5000"
response = requests.post(url, files=files)