title: writing python request using "post" to flask server as binary file
body: <p>My client use this code to upload file and some json to a flask server : </p>
  
  <pre><code>datas = {'CurrentMail': "AA", 'STRUserUUID1': "BB", 'FirstName': "ZZ",     'LastName': "ZZ",  'EE': "RR", 'JobRole': "TT"  }
  
  #sending user infos to app server using python "requests"
  url = "http://10.100.2.6:80/customerupdate"
  def send_request():
      payload = datas
      local_file_to_send = 'user_picture.jpg' 
      files = {
       'json': (None, json.dumps(payload), 'application/json'),
       'file': (os.path.basename(local_file_to_send), open(local_file_to_send, 'rb'), 'application/octet-stream')
  }
  r = requests.post(url, files=files)
  
  send_request()
  </code></pre>
  
  <p>on My server</p>
  
  <p>On Flask Server side :</p>
  
  <pre><code>import sys, os, logging, time, datetime, json, uuid, requests, ast
  from flask import Flask, request , render_template
  from werkzeug import secure_filename
  from werkzeug.datastructures import ImmutableMultiDict
  from flask_restful import Resource, Api
  
  app = Flask(__name__)
  api = Api(app)
  app.debug = True
  
  class OPERATIONS(Resource):
      @app.route('/',methods=['GET'])
      def hello_world():
          return 'Hello World!'
  
      @app.route('/customerupdate',methods=['GET','POST'])
      def customerupdate():
          print "************DEBUG 1 ***********"
          RequestValues = request.values
          print RequestValues
          print "************DEBUG 2 ***********"
          RequestForm = request.form
          print RequestForm
          print "************DEBUG 2-1 ***********"
          so = RequestForm
          json_of_metadatas = so.to_dict(flat=False)
          print json_of_metadatas
          print "************DEBUG 2-2 ***********"
          MetdatasFromJSON = json_of_metadatas['json']
          print MetdatasFromJSON          
          print "************DEBUG 2-3 ***********"
          MetdatasFromJSON0 = MetdatasFromJSON[0]
          print MetdatasFromJSON0
          print "************DEBUG 3-5 ***********"
          strMetdatasFromJSON0 = str(MetdatasFromJSON0)
          MetdatasDICT = ast.literal_eval(strMetdatasFromJSON0)
          print MetdatasDICT
          print "************DEBUG 3-5 ***********"
          for key in MetdatasDICT :
              print "key: %s , value: %s" % (key, MetdatasDICT[key])
          print "************DEBUG 4 ***********"
          f = request.files['file']
          f.save(secure_filename(f.filename))
          print "FILE SAVED LOCALY"
          return 'JSON of customer posted'
  </code></pre>
  
  <p>So I could save some metadatas and the file itself on my server. What I would like to do now is to save the complete "POST" as raw binary file. </p>
  
  <p>AS mentionned is some thread in SO, I tried using :</p>
  
  <pre><code>        # test 1
          foo = request.get_data()
          #TEST 2
          foo = request.files
          #test 3
          foo = request.stream.read()
  
          f = request.files['file']
          fname = f.filename
          #Using Hexa for the binary name and prefix bin
          fname_hexa = fname.encode("hex")
          bin_file_name="%s_%s"%("bin",fname_hexa)
          with open(bin_file_name, 'wb') as output_file_bin:
              output_file_bin.write(**foo**)
              output_file_bin.close()
  </code></pre>
  
  <p>But I get an empty file whatever the method used to right "foo" : get_data() and stream.read()
  If I use only "files", I got an error message from my flask server : </p>
  
  <pre><code>  TypeError: argument 1 must be convertible to a buffer, not ImmutableMultiDict
  </code></pre>