Skip to content

Commit 251c597

Browse files
committed
server: send correct Accept=application/fhir+json HTTP headers
First and foremost, stop requesting application/json for FHIR objects. The correct type is application/fhir+json. The spec says: "If a client provides a generic mime type in the Accept header (application/xml, text/json, or application/json), the server SHOULD respond with the requested mime type, using the XML or JSON formats described in this specification as the best representation for the named mime type (except for binary - see the note on the Binary resource)." So this was only ever working by accident, and wasn't correct for Binary resources, which when provided a non-fhir+json Accept header are supposed to return their binary content, not the FHIR object. So now we send the right header. Also for DELETE requests, send signed headers if possible. Fixes #191
1 parent 9556bd3 commit 251c597

File tree

2 files changed

+9
-13
lines changed

2 files changed

+9
-13
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ patient = Patient.read('2cda5aad-e409-4070-9a15-e1c35c46ed5a', smart.server)
5353
print(patient.birthDate.isostring)
5454
# '1992-07-03'
5555
print(smart.human_name(patient.name[0]))
56-
# 'Mr. Geoffrey Abbott'
56+
# 'Mr. steve Smith'
5757
```
5858
If this is a protected server, you will first have to send your user to the authorization endpoint to log in.
5959
Just call `smart.authorize_url` to obtain the correct URL.
@@ -90,7 +90,7 @@ from fhirclient.models.patient import Patient
9090
smart = server.FHIRServer(None, 'https://r4.smarthealthit.org')
9191
patient = Patient.read('2cda5aad-e409-4070-9a15-e1c35c46ed5a', smart)
9292
print(patient.name[0].given)
93-
# ['Geoffrey']
93+
# ['steve']
9494
```
9595

9696
##### Search Records on Server

fhirclient/server.py

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -159,19 +159,18 @@ def request_json(self, path, nosign=False):
159159
:throws: Exception on HTTP status >= 400
160160
:returns: Decoded JSON response
161161
"""
162-
headers = {'Accept': 'application/json'}
163-
res = self._get(path, headers, nosign)
162+
res = self._get(path, nosign=nosign)
164163

165164
return res.json()
166165

167-
def request_data(self, path, headers={}, nosign=False):
166+
def request_data(self, path, headers=None, nosign=False):
168167
""" Perform a data request data against the server's base with the
169168
given relative path.
170169
"""
171-
res = self._get(path, headers, nosign)
170+
res = self._get(path, headers=headers, nosign=nosign)
172171
return res.content
173172

174-
def _get(self, path, headers={}, nosign=False):
173+
def _get(self, path, headers=None, nosign=False):
175174
""" Issues a GET request.
176175
177176
:returns: The response object
@@ -184,7 +183,8 @@ def _get(self, path, headers={}, nosign=False):
184183
'Accept-Charset': 'UTF-8',
185184
}
186185
# merge in user headers with defaults
187-
header_defaults.update(headers)
186+
if headers:
187+
header_defaults.update(headers)
188188
# use the merged headers in the request
189189
headers = header_defaults
190190
if not nosign and self.auth is not None and self.auth.can_sign_headers():
@@ -251,10 +251,6 @@ def post_as_form(self, url, formdata, auth=None):
251251
:throws: Exception on HTTP status >= 400
252252
:returns: The response object
253253
"""
254-
headers = {
255-
'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8',
256-
'Accept': 'application/json',
257-
}
258254
res = self.session.post(url, data=formdata, auth=auth)
259255
self.raise_for_status(res)
260256
return res
@@ -276,7 +272,7 @@ def delete_json(self, path, nosign=False):
276272
headers = self.auth.signed_headers(headers)
277273

278274
# perform the request but intercept 401 responses, raising our own Exception
279-
res = self.session.delete(url)
275+
res = self.session.delete(url, headers=headers)
280276
self.raise_for_status(res)
281277
return res
282278

0 commit comments

Comments
 (0)