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=<MixedContent.sh" \
     -F "json_data={\"username\":\"xyz\",\"password\":\"xyz\"}" \
     -X POST http://127.0.0.1:5000/add

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

Flask Handling this

Inside my flask app.py

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)
        sleep(1)
        tot = e.count()
        doc = {}
        doc['text'] = request.data.decode('utf-8')

        try:

            res=e.es.index(index=e.IDX, 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:
            flash(str(err))
    elif request.headers['Content-Type'] == 'multipart/mixed':
        return "400 Mixed"
    elif request.headers['Content-Type'].startswith('multipart/form-data'):
        print("multi-form data")
        json_data=request.form.get("json_data")
        jd = json.loads(json_data)
        file_data=request.form.get("file")


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