client-py icon indicating copy to clipboard operation
client-py copied to clipboard

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

Open themantalope opened this issue 4 years ago • 0 comments

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.

themantalope avatar Dec 09 '20 15:12 themantalope