Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

attaching binary data in DocumentReference - Attachment expects data to be <class 'str'>? #92

Open
themantalope opened this issue Dec 9, 2020 · 0 comments

Comments

@themantalope
Copy link

Hi all,

I'm very new to FHIR and I'm learning. Any help is much appreciated. I'm trying to attach a PDF file to a document reference for a patient. I'm using the following code:

from fhirclient.models.patient import Patient
from fhirclient.models.documentreference import DocumentReference
from fhirclient.models.attachment import Attachment
from fhirclient.models.binary import Binary
from fhirclient import client

# client to local HAPI FHIR server
smart = client.FHIRClient({'api_base':'http://localhost:8080/baseDtsu3', 'app_id':''})

smart.prepare()

# get a patient
patient = Patient.read('1', smart.server)

# make a document reference
dr = DocumentReference()
dr.subject = patient

# make attachment
a = Attachment()
a.contentType = 'application/pdf'
# read in pdf file
pdf_file = ....
pdf_data = None
with open(pdf_file, 'rb') as f:
  pdf_data = f.read()

a.data = pdf_data
dr.attachment = a

# now try to post
dr.create(smart.server)

When I try running this code, I get an error stating that Expecting property "data" on <class 'fhirclient.models.attachment.Attachment'> to be <class 'str'>, but is <class 'bytes'>. How should the binary data be stored?

I could do something like:

pdf_bin = None
import base64
with open(pdf, 'rb') as f:
 pdf_bin = f.read()
 pdf_b64 = base64.b64encode(pdf_bin)
 pdf_b64_m = pdf_b64.decode('utf-8')

Where now pdf_b64_m is a UTF-8 encoded string. Is this the correct way to store the data? I would then be able to re-encode it back to binary, but I want to make sure that this is correct so other programs that download the data from the server can interpret the PDF correctly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant