【问题标题】:AuthenticationRequired response when trying to access a public FHIR (RESTFul) endpoint via Python尝试通过 Python 访问公共 FHIR (RESTFul) 端点时的 AuthenticationRequired 响应
【发布时间】:2020-07-31 20:21:23
【问题描述】:

我正在学习需要访问公共 FHIR 端点的课程。我正在尝试通过 Python 执行此操作,但收到 407 AuthenticationRequired 响应,而这应该是公共服务器。

我已与课程管理员核实,并确信这应该是公共服务器。我可以通过浏览器轻松处理获取请求(请注意,这都是虚构的患者数据):如果我访问 URI http://fhir.hl7fundamentals.org/r4/Patient/17120,我会在浏览器中得到预期的响应。

HTTP 200 OK 响应标头 X-Powered-By: HAPI FHIR 3.7.0-SNAPSHOT REST 服务器(FHIR 服务器;FHIR 4.0.0/R4)内容类型: 应用程序/fhir+xml;charset=utf-8

响应正文 { "resourceType": “患者”,“id”:“17120”,“元”:{ “版本号”:“22”, "lastUpdated": "2020-07-31T03:55:59.144+00:00" }.....

当我尝试通过 Python 对同一资源发出 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-->'

如果这是一个公共服务器,这意味着我可能在 Python 方面做错了 - 关于我可能做错了什么或为什么我得到 AuthenticationRequired 的任何想法?

【问题讨论】:

  • 它适用于我,在 Python 3.8 和 2.7 中,尽管 JSON 无效。如果我将?_format=xml 添加到 URL,我会得到有效的 XML。您是否在与浏览器相同的机器上运行 Python 代码?
  • 您使用的是浏览器代理而不是 python?
  • 407 不是由服务器发送的 - 如果您需要对自己进行身份验证,服务器将发送 401。如果您查找它,它是“需要 407 代理身份验证”。所以有一个代理在路上。
  • @MirjamBaltus 这最终成为了答案。

标签: python rest python-requests hl7-fhir


【解决方案1】:

使用 Python 3.6+ 和 Jupyter Notebooks(可选)

使用 Python 请求库的基本 FHIR Restful API

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'
    }

获取资源

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...

验证输出

状态码 = 200

叙述: FERCAM ID # 19282

原始 JSON:

{ "resourceType": "病人", “id”:“17120”, “元”:{ “版本号”:“22”, “上次更新”:“2020-07-31T03:55:59.144+00:00” }, “文本”: { “状态”:“生成”, "div": " \n FERCAM ID # 19282 \n " }, “标识符”:[ { “系统”:“https://www.hospitalitaliano.org.ar/Patient”, “价值”:“123456” } ], “姓名”: [ { "family": "Campos_new", “给定”:[ “费尔南多新” ] } ], “地址”: [ { “线”: [ “1111葡萄糖” ], “城市”:“安娜堡”, “州”:“密歇根西部”, “国家”:“美国” } ], “管理组织”:{ “参考”:“组织/2” } }

【讨论】:

  • 这不是回答我提出的问题,但我肯定会对很多人有用,所以 +1。
猜你喜欢
  • 2020-06-26
  • 2018-05-25
  • 2018-01-19
  • 2020-07-11
  • 2019-08-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多