You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Python(Databricks)调用Azure FHIR API时Bearer Token头部报错

问题解决方案

你的代码报错原因很明确:Authorization请求头的值被意外变成了元组类型,这是因为你在赋值语句末尾多写了一个逗号:

headers["Authorization"] = "Bearer " + token,  # 这里的逗号导致值变成了('Bearer xxx',) 元组

Python中,单个值后面加逗号会被解析为元组,而requests库要求请求头的值必须是字符串或字节类型,所以抛出了InvalidHeader错误。

修正后的完整代码

import requests
from requests.structures import CaseInsensitiveDict

url = 'https://workspace-test-2.fhir.azurehealthcareapis.com/Patient/'

token = 'eyJ0eXAiOiJKV1QiLCJ'  # 注意替换为实际完整的有效令牌

headers = CaseInsensitiveDict()
headers["Accept"] = "application/json"
headers["Authorization"] = "Bearer " + token  # 去掉末尾的逗号
headers["Content-Type"] = "application/json"

# 定义请求体数据
data = """
{"resourceType":"Patient","identifier":[{"use":"official","type":{"coding":[{"system":"http://terminology.hl7.org/CodeSystem/v2-0203","code":"NNcol"},{"system": "https://registraduria.gov.co/identificaciones","code": "CC","display": "Cedula de Ciudadania"}]},"value":"DSA2FSA1319832-K"}]}
"""

resp = requests.post(url, headers=headers, data=data)

print(resp.status_code)
print(resp.text)  # 可增加这行查看返回详情,方便调试

额外调试建议

  • 确保token是完整且有效的JWT令牌,示例中的令牌是截断状态,实际使用时要替换为完整值
  • 可以打印headers查看结构,确认所有请求头的值都是字符串类型:print(headers)

内容的提问来源于stack exchange,提问作者Gonza

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.22 05:33:57