Automated patient document upload

Dear all, this is my first post here after 2 days of trying to get this to work without success.
I’m migrating into OpenEMR and got as far as cloning current patients with their respective ids into OpenEMR and checking what files exist in the old and new system.

That leaves 1 critical task:

  • upload a file into a patient with a specific name

I’m writing this patient manager in python3
I looked into openemr/C_Document.class.php at master · openemr/openemr · GitHub
to see why it isn’t working but didn’t get any further than validating that ‘file’ is used to receive the file.

upload script (crude stuff here :wink:

                    print("will be uploaded")
                    uploadFiles = {'file': open(filePath, 'rb')}
                    url = baseUrl + '/controller.php?document&upload&patient_id=' + clientEmrId + '&parent_id=3&'
                    extraHeaders = {
                        'patient_id': clientEmrId,
                        'parent_id': '3',
                        'category_id': '3',
                        'process': 'true'
                    }
                    with requests.Session() as session:
                        login = session.post(loginUrl, data=payload)
                        r = session.post(url, files=uploadFiles, data=extraHeaders)
                        print(session.cookies.get_dict())
                        print(url,extraHeaders,uploadFiles)

Does anyone know what headers / cookies / body magic is required to make this work?

After some mitm decrypting and tcpdumping I found these:

type: multipart/form-data, Boundry: "----Webkitboundry....."

content-disposition form-data name=file[]       filename="chosen1.file" Content-Type: application/...
content-disposition form-data name=file[]       filename="chosen2.file" Content-Type: application/...
content-disposition form-data name=destination  (chosen_dest_filename)
content-disposition form-data name=patient_id   (just the number)
content-disposition form-data name=category_id  (3 = medical record)
content-disposition form-data name=process      (true)

Now let’s get coding maybe this will be the end of the puzzle…

Fixed it :smile:

print("will be uploaded")
uploadFiles = {'file[]': open(filePath, 'rb')}
url = baseUrl + '/controller.php?document&upload&patient_id=' + clientEmrId + '&parent_id=3&'
extraHeaders = {
    'destination': fileName,
    'patient_id': clientEmrId,
    'category_id': '3',
    'process': 'true'
}
with requests.Session() as session:
    login = session.post(loginUrl, data=payload)
    r = session.post(url, files=uploadFiles, data=extraHeaders)
1 Like

hi @aapjeisbaas ,
Is this code in the codebase that needs to be fixed or is this a python code sample to bring in patient data?
thanks,
-brady

No this is not in the codebase it is a part of a small migration / import project I’m working on.
If it’s stable enough for other to reuse I’ll publish it and link it in here.
I’m no programmer by trade, just a sysadmin that from time to time writes some tooling.

1 Like

Hello
How does this script work?
I have a scanner that scans and scans a document and names the file based on the patient ID and document type.
Example : 214_hippa.pdf
Can I use your script to upload this document from a folder to Openemr?

Thanks