I have demographics information extracted for some people in the form of list of python dictionaries(each dict for an individual). Also I need to upload the document from where I extracted the data (pdf/word). I tried Multipart form submission using Python requests which because of some reason does not seem to work.
The API expects two keys 'files' and 'data' 'files' is a list of file objects 'data' is a list of dicts which is stringified using json.dumps (API requirements)
pay_part= [{"umr":"","age":"","gender":"","first_name":"","middle_name":"","last_name":"","phone":"","address":"","admission_date":"","lab":"","discharge_date":"","ip_number":"","diagnosis":"","reason":"","treatment":"","medications":"","expired_date":"","instructions":"","review_date":"","procedure":"","notes":"","physician":"","filename":""},{"umr":"","age":"","gender":"","first_name":"","middle_name":"","last_name":"","phone":"","address":"","admission_date":"","lab":"","discharge_date":"","ip_number":"","diagnosis":"","reason":"","treatment":"","medications":"","expired_date":"","instructions":"","review_date":"","procedure":"","notes":"","physician":"","filename":""}]
multipart_data = MultipartEncoder(
fields={
"file":[('file.docx',open('13427.docx', 'rb'),'text/plain'),
('file.docx',open('13427.docx', 'rb'),'text/plain')],
"payload": json.dumps(pay_part)
}
)
response = requests.post(url, data=multipart_data, headers={'Content-Type': 'multipart_data.content_type; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW', 'userid': sUserID,'metaid': metaid,'postman-token':postmanToken})
print(response.text)
While forming the multipart form object I get an error "AttributeError: 'tuple' object has no attribute 'encode'".
I believe this has to do something with creating file objects as a binary and storing in list.
Thanks in advance!