【问题标题】:Upload Image using POST form data in Python-requests在 Python 请求中使用 POST 表单数据上传图像
【发布时间】:2015-05-20 04:15:54
【问题描述】:

我正在使用微信 API ... 在这里,我必须使用此 API 将图像上传到微信服务器 http://admin.wechat.com/wiki/index.php?title=Transferring_Multimedia_Files

url = 'http://file.api.wechat.com/cgi-bin/media/upload?access_token=%s&type=image'%access_token
files = {
    'file': (filename, open(filepath, 'rb')),
    'Content-Type': 'image/jpeg',
    'Content-Length': l
}
r = requests.post(url, files=files)

我无法发布数据

【问题讨论】:

    标签: python python-3.x curl python-requests wechat


    【解决方案1】:

    来自微信api文档:

    curl -F media=@test.jpg "http://file.api.wechat.com/cgi-bin/media/upload?access_token=ACCESS_TOKEN&type=TYPE"
    

    将上面的命令翻译成python:

    import requests
    url = 'http://file.api.wechat.com/cgi-bin/media/upload?access_token=ACCESS_TOKEN&type=TYPE'
    files = {'media': open('test.jpg', 'rb')}
    requests.post(url, files=files)
    

    文档:https://docs.python-requests.org/en/master/user/quickstart/#post-a-multipart-encoded-file

    【讨论】:

    • 我对 api-rest 的使用有疑问,当我尝试发送请求时,响应是“消息”:“表单数据请求中没有提供图像”,我的请求是下一个: curl -X POST --header "Content-Type: multipart/form-data" --head er "Accept: application/json" --header "api_key: zzzz" --header "Content-Type: image/jpg" --header "Content-Disposition: form-data" -F media=2016-09-14-154558.jpg "xxxx"
    • 如果有人需要以file object 的形式提供数据,例如,如果您获得some_image = requests.get(url_to_some_image) 之类的图像并希望将此图像发布到某个地方,则需要将图像设置为@987654326 @。为此,根据 Kev 的回答,您需要制作 files = {'media': io.BytesIO(some_image.content)}
    • 嗯,这对我不起作用。该请求返回一个 200 代码,但响应消息为“无”,并且它没有到达其目的地。我正在打印缓冲区读取,并且可以通过打印出缓冲区字符串来确认它以您编写的方式抓取文件。
    【解决方案2】:

    当我想从 Python 将图像文件发布到 rest API(虽然不是微信 API)时,我遇到了类似的问题。我的解决方案是使用“数据”参数以二进制数据而不是“文件”形式发布文件。 Requests API reference

    data = open('your_image.png','rb').read()
    r = requests.post(your_url,data=data)
    

    希望这适用于您的情况。

    【讨论】:

      【解决方案3】:

      Rest API 将图像从主机上传到主机:

      import urllib2
      import requests
      
      api_host = 'https://host.url.com/upload/'
      headers = {'Content-Type' : 'image/jpeg'}
      image_url = 'http://image.url.com/sample.jpeg'
      
      img_file = urllib2.urlopen(image_url)
      
      response = requests.post(api_host, data=img_file.read(), headers=headers, verify=False)
      

      您可以使用选项 verify 设置为 False 来省略 HTTPS 请求的 SSL 验证。

      【讨论】:

        【解决方案4】:
        import requests
        
        image_file_descriptor = open('test.jpg', 'rb')
        # Requests makes it simple to upload Multipart-encoded files 
        files = {'media': image_file_descriptor}
        url = '...'
        requests.post(url, files=files)
        image_file_descriptor.close()
        

        不要忘记关闭描述符,它可以防止错误:Is explicitly closing files important?

        【讨论】:

        • 你的files来自哪里?
        【解决方案5】:

        使用这个sn-p

        import os
        import requests
        url = 'http://host:port/endpoint'
        with open(path_img, 'rb') as img:
          name_img= os.path.basename(path_img)
          files= {'image': (name_img,img,'multipart/form-data',{'Expires': '0'}) }
          with requests.Session() as s:
            r = s.post(url,files=files)
            print(r.status_code)
        

        【讨论】:

        • 你真是个天才
        • Expires 有什么作用?
        【解决方案6】:

        如果您要将图像作为 JSON 的一部分与其他属性一起传递,您可以使用下面的 sn-p。
        客户端.py

        import base64
        import json                    
        
        import requests
        
        api = 'http://localhost:8080/test'
        image_file = 'sample_image.png'
        
        with open(image_file, "rb") as f:
            im_bytes = f.read()        
        im_b64 = base64.b64encode(im_bytes).decode("utf8")
        
        headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
          
        payload = json.dumps({"image": im_b64, "other_key": "value"})
        response = requests.post(api, data=payload, headers=headers)
        try:
            data = response.json()     
            print(data)                
        except requests.exceptions.RequestException:
            print(response.text)
        

        server.py

        import io
        import json                    
        import base64                  
        import logging             
        import numpy as np
        from PIL import Image
        
        from flask import Flask, request, jsonify, abort
        
        app = Flask(__name__)          
        app.logger.setLevel(logging.DEBUG)
          
          
        @app.route("/test", methods=['POST'])
        def test_method():         
            # print(request.json)      
            if not request.json or 'image' not in request.json: 
                abort(400)
                     
            # get the base64 encoded string
            im_b64 = request.json['image']
        
            # convert it into bytes  
            img_bytes = base64.b64decode(im_b64.encode('utf-8'))
        
            # convert bytes data to PIL Image object
            img = Image.open(io.BytesIO(img_bytes))
        
            # PIL image object to numpy array
            img_arr = np.asarray(img)      
            print('img shape', img_arr.shape)
        
            # process your img_arr here    
            
            # access other keys of json
            # print(request.json['other_key'])
        
            result_dict = {'output': 'output_key'}
            return result_dict
          
          
        def run_server_api():
            app.run(host='0.0.0.0', port=8080)
          
          
        if __name__ == "__main__":     
            run_server_api()
        

        【讨论】:

          【解决方案7】:

          您可以使用 GitHub 风格的 Markdown 格式创建表格。

          |一个标题 |另一个标题 | | -------- | -------------- | |第一 |行 | |第二 |行 | 标题行是必需的,并且必须后跟具有相同单元格数的分隔符行 单元格由竖线 (|) 符号分隔 通过在分隔线的左侧、右侧或两侧放置一个 : 来设置表格列的对齐方式。

          left center right
          One Two Three

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2015-11-20
            • 1970-01-01
            • 2019-08-29
            • 1970-01-01
            • 2021-08-14
            • 1970-01-01
            • 2018-04-20
            相关资源
            最近更新 更多