0
votes

I'm taking a course in which we need to access a public FHIR endpoint. I'm trying to do this via Python, but am receiving a 407 AuthenticationRequired response, when this is supposed to be a public server.

I've checked with the course administrators and have been assured this is supposed to be a public server. I am able to easily process a get request via my browser (note this is all fabricated patient data): if I access the URI http://fhir.hl7fundamentals.org/r4/Patient/17120, I get an expected response within my browser.

HTTP 200 OK Response Headers X-Powered-By: HAPI FHIR 3.7.0-SNAPSHOT REST Server (FHIR Server; FHIR 4.0.0/R4) Content-Type: application/fhir+xml;charset=utf-8

Response Body { "resourceType": "Patient", "id": "17120", "meta": { "versionId": "22", "lastUpdated": "2020-07-31T03:55:59.144+00:00" }.....

When I try a get request to the same resource via Python, I get AuthenticationRequired.

import requests
r = requests.get('http://fhir.hl7fundamentals.org/r4/Patient/17120')
r.content

Out[24]: b'<!--Title-->\nAuthenticationRequired\n<!--/Title-->\n<!--Content-->\n407\n<!--/Content-->'

If this is a public server, this implies I may be doing something wrong on the Python side of things - any thoughts on what I might be doing wrong or why I get AuthenticationRequired?

1
It works for me, in Python 3.8 and 2.7, although the JSON is invalid. If I add ?_format=xml to the URL I get valid XML. Are you running the Python code from the same machine as the browser? - Dan-Dev
Are you using a proxy for the browser and not python? - Dan-Dev
407 is not sent by the server - the server would send 401 if you need to authenticate yourself. If you look it up, it is "407 Proxy Authentication Required". So there's a proxy in the way. - Mirjam Baltus
@MirjamBaltus this ended up being the answer. - Julian Drago

1 Answers

1
votes

using Python 3.6+ and Jupyter Notebooks (which is optional)

Basic FHIR Restful API Using Python Requests Library

from requests import get,post
from json import loads, dumps
from IPython.display import display, HTML, Markdown

ref_server = 'http://fhir.hl7fundamentals.org/r4'
r_type ='Patient'
r_id = '17120'
headers = {
    'Accept':'application/fhir+json',
    'Content-Type':'application/fhir+json'
    }

Fetch a resource

def fetch_me(r_type, r_id):
    print(f'fetching FHIR {r_type} with id = {r_id} from {ref_server}...')
    r = get(f'{ref_server}/{r_type}/{r_id}', headers = headers)
    display(HTML(
        '<h1>Validation output</h1>'
        f'<h3>Status Code = {r.status_code}</h3>'
        f'<strong>Narrative:</strong>\n {r.json()["text"]["div"]}\n'
        f'<strong>Raw JSON:</strong>\n<pre>{dumps(r.json(),indent=4)}</pre>'
        ))


fetch_me(r_type, r_id)        
fetching FHIR Patient with id = 17120 from http://fhir.hl7fundamentals.org/r4...

Validation output

Status Code = 200

Narrative:

Raw JSON:

{
    "resourceType": "Patient",
    "id": "17120",
    "meta": {
        "versionId": "22",
        "lastUpdated": "2020-07-31T03:55:59.144+00:00"
    },
    "text": {
        "status": "generated",
        "div": " \n                        FERCAM Id # 19282 \n                    "
    },
    "identifier": [
        {
            "system": "https://www.hospitalitaliano.org.ar/Patient",
            "value": "123456"
        }
    ],
    "name": [
        {
            "family": "Campos_new",
            "given": [
                "Fernando_new"
            ]
        }
    ],
    "address": [
        {
            "line": [
                "1111 Glucose"
            ],
            "city": "Ann Arbor",
            "state": "MichiganWest",
            "country": "USA"
        }
    ],
    "managingOrganization": {
        "reference": "Organization/2"
    }
}