curl file and parameters read in python

sending the file and json data

I use this in bash

# Note 2 variables file and json
# file is loaded using < redirection
# Json_Data is a hand crafted dictionary
curl -X POST -H "Content-Type: multipart/form-data" \
     -F "file=<" \
     -F "json_data={\"username\":\"xyz\",\"password\":\"xyz\"}" \
     -X POST

This will be fine for TEXT files, but will fail for Binary I think.

Flask Handling this

Inside my flask

the /add entry point looks like this.

binary is for --binary @file.bin in the CURL - not for mixed form types.

def add():
    if request.headers['Content-Type'] == 'text/plain':
        e = esq("es_doc", 9200)
        tot = e.count()
        doc = {}
        doc['text'] ='utf-8')


  , doc_type=e.TYPE, id=tot, body=json.dumps(doc, ensure_ascii=False))
            print("Loaded Document {}".format(tot))
            tot = tot + 1
            return jsonify(id=tot,status=200)

        except Exception as err:
    elif request.headers['Content-Type'] == 'multipart/mixed':
        return "400 Mixed"
    elif request.headers['Content-Type'].startswith('multipart/form-data'):
        print("multi-form data")
        jd = json.loads(json_data)

        return "400 Form-Mixed.... Possible"
        return "415 Unsupported Media Type ;)"