Stream: python
Topic: HTTP response header and location
Bob Milius (Apr 07 2018 at 13:29):
I've been exploring the fhirclient
python package from the smart client-py repo, and wonder what the correct way to figure out the Location
of the resource created after posting it to a server. For example posting to an open server with
response = mypatient.create(smart.server) print(json.dumps(response, indent=4))
gives me
{ "resourceType": "OperationOutcome", "text": { "status": "generated", "div": "<div xmlns=\"http://www.w3.org/1999/xhtml\"><h1>Operation Outcome</h1><table border=\"0\"><tr><td style=\"font-weight: bold;\">INFORMATION</td><td>[]</td><td><pre>Successfully created resource "Patient/2516/_history/1" in 3ms</pre></td>\n\t\t\t\t\t\n\t\t\t\t\n\t\t\t</tr>\n\t\t</table>\n\t</div>" }, "issue": [ { "severity": "information", "code": "informational", "diagnostics": "Successfully created resource \"Patient/2516/_history/1\" in 3ms" } ] }
I can parse the issue.diagnostics
, but is there a structured way to get the Location
field from a HTTP response header using the fhirclient package?
Pascal Pfiffner (Apr 08 2018 at 05:36):
I believe exposing response headers has not been implemented yet. You'd have to hook into the create
and update
methods in fhirabstractresource
. The line ret = srv.post_json(...)
returns a "requests" object that you can inspect to learn more about the response.
Bob Milius (Apr 08 2018 at 14:40):
thanks, @Pascal Pfiffner , I''ll look there.
Bob Milius (Apr 09 2018 at 00:35):
With the fhirclient
package as distributed from https://github.com/smart-on-fhir/, mypatient.create(smart.server)
returns the JSON formatted Content
. I wanted access to the full HTTP header response, so I made the following simple change to the create
, update
, and delete
methods in fhirclient/models/fhirabstractresource
so that the entire requests.models.Response
object is returned.
from
ret = srv.post_json(self.relativeBase(), self.as_json()) if len(ret.text) > 0: return ret.json()
to
ret = srv.post_json(self.relativeBase(), self.as_json()) if len(ret.text) > 0: # return the full requests.Response object return ret # return ret.json()
Now I can replicate the previous behavior of response
by using response.json()
and also access the headers by using response.headers
, e.g., response.headers.['location']
Now testing to see if it breaks anything.
thanks again, @Pascal Pfiffner , for pointing me in the right direction.
Last updated: Apr 12 2022 at 19:14 UTC