Flask restful the browser (or proxy) sent a request that this server could not understand

Hello, After several attempts, got which was the issue, but the solution seems hard to find. I modified Javascript to set ALL headerd necessary to send data via FormData(), but Flask gave the same error, 400 bad request. But I was sure data was transferred to the server. I tried to look into request method and I discovered that Flask used request.data method instead of request.form, and data was converted into a string type. I read that this happens when Flask does not recogize Content-type headers.

Is there any way to force data to fill request.form inestead of request.data?

Thank you!

//////////////////////////////////////////////////////////////////// JAVASCRIPT

function loadDoc(id_dipartimento, imgfile) { var meta = document.getElementsByTagName("meta"); for(var i=0; i< meta.length; i++) { if(meta[i].getAttribute('content') != "") { var csrftoken = meta[i].getAttribute('content'); } } var formData = new FormData(); formData.append("id_dipartimento", id_dipartimento); formData.append("imgfile", imgfile); var xhttp = new XMLHttpRequest(); xhttp.open("POST", "/sendFiles", true); xhttp.setRequestHeader('Content-Type', 'application/octet-stream'); xhttp.setRequestHeader('enctype','multipart/form-data'); xhttp.setRequestHeader("X-CSRFToken", csrftoken); xhttp.setRequestHeader('X-Upload-Content-Length', imgfile.size); xhttp.setRequestHeader('Content-length', imgfile.size); xhttp.setRequestHeader('Content-transfer-encoding', 'binary'); xhttp.send(formData); return xhttp; }

//////////////////////////////////////////////////////////////////////////////////////// PYTHON

@app.route('/sendFiles', methods=['POST']) @login_required def sendFiles(): print request.data.keys()

//////////////////////////////////////////////////////////////////////////////////////// SERVER.LOG

2016-07-31 10:05:29 -----------------------------1675367026141005455785498554#015#012Content-Disposition: form-data; name="id_dipartimento"#015#012#015#01210#015#012-----------------------------1675367026141005455785498554#015#012Content-Disposition: form-data; name="imgfile"; filename="knife1.jpeg"#015#012Content-Type: image/jpeg#015#012#015#012���� 2016-07-31 10:05:29 �#017ML%flk��2)��t 2016-07-31 10:05:29 �����h4/,C����@�#020#020.Ӗ$�#002#030#022��#026#026Ƒ 2016-07-31 10:05:29 Ѐ��� �#010#011v�#004cH!#005�#004 li#024##026@!#005�����#004#021�#030 lE�#030#004#025#020@H��5��#032�#031TdD�#024��@"5�[#031�3B�[F��DvM�, �8�#004F�4(�#004F�#011#021�M�DA"4%cLB��Da#022����Q#022�#033iDNhU��eZvP#026���.Y̠Z]ZX#023*#004#010@B��',I#024#004 %�#010#010[#032D#002#020#022#001#010#010[#032E��#026@!#001#003e"���)D,�06U �C#002#010�X#033(�#010�Y#022BR��#026�o#003#026h7)�3��C��#036�!nqF�r����#033��#014���{#007J�8SA�[�`�#017��S;�'W#020�#016�np#007#016���0N#035�#033�g�d�Խ�h���#016#035�#033�ez��?�ϢL?HN,U�6#024#026AU�h#002/�#034�*#016���#017o#033N���Ǹ�N#031�#037�ez��?�����#013U/�#002q%�uA���G�Q��@���5&��#032�q��Mfˊ5#0230��0`�4���"�#026a�#013���[��Ǹ��g�)�����Vx.9Iz]1#015��z#026UE 2016-07-31 10:05:29 2016-07-31 10:05:29 4�™SS~��:{����ܬl/#021����#035"��P3�ZMIX���hE0�#020A�J��#015��c��#036�#033՟���/՟���6�8��� 2016-07-31 10:05:29 #015��S;�'5#017c��[8ܭ0֋r�������a.#025nq��r���(+x#031��#017��#022������ܭd|ycJܒI$rI$�#030I$$I1$LbZU��1i-$�#030ZI&1V���Ĵ��IKI$�#030I$$I1$LbI$�#030I$$I1���#015 2016-07-31 10:05:29 -----------------------------1675367026141005455785498554--#015