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

Apache Airflow SimpleHttpOperator使用Bearer Token请求API连接超时

问题现象

对接需Bearer Token授权的API服务,Postman中已验证Token可正常调用接口,但Airflow中使用SimpleHttpOperator发起POST请求时报错,错误信息如下:

建立新连接失败:[错误号 110] 连接超时
处理上述异常过程中触发次级异常:已达到最大重试次数

问题相关实现代码

from airflow import DAG
from airflow.operators.http_operator import SimpleHttpOperator
from airflow.operators.python import PythonOperator
import json

def process_source_claim_req() -> SimpleHttpOperator:
     data = {'Envelope': {'ConfigurationNumber': '#BG001', 'ProcessingMode': 'Process'}, 'Claim': {'Header': {'AmbulancePickupZip': '12345', 'BeneficiaryId': '961005801WXYZ', 'BeneficiaryZip': '12345', 'BillingZip': '12345', 'BirthDate': '01/01/1900', 'ClaimId': '26202872972300', 'DxCode01': 'M545', 'FederalTaxId': '046001309', 'IcdVersionIndicator': '0', 'MedicaidState': '', 'ProviderType': 'MC09', 'ServiceFacilityZip': '12345', 'Sex': 'U'}, 'Lines': [{'LineNumber': '1', 'Charges': '2211.48', 'DateOfService': '10/01/2020', 'EndDateOfService': '10/01/2020', 'Hcpcs': 'A0427', 'Modifier01': 'SH', 'PlaceOfService': '41', 'RenderingNpi': '1700815941', 'Units': '1.00'}, {'LineNumber': '2', 'Charges': '435.20', 'DateOfService': '10/01/2020', 'EndDateOfService': '10/01/2020', 'Hcpcs': 'A0425', 'Modifier01': 'SH', 'PlaceOfService': '41', 'RenderingNpi': '1700815941', 'Units': '12.80'}]}}

     task_func = SimpleHttpOperator(
        task_id='process_source_claim_req',
        http_conn_id='source_api',
        method='POST',
        data=json.dumps(data),
        headers={"Authorization": "Bearer Hqasdsdhasdhsdqigd39318",
                  "Content-Type": "application/json"}
        )

     return task_func
排查与修复步骤
  • 优先核对Airflow连接配置
    这类Postman可通、Airflow内请求超时的问题,绝大多数是http_conn_id配置错误导致。进入Airflow UI的「Admin-Connections」页面找到ID为source_api的连接配置,逐一核对:
    • Host字段必须填写API的完整根地址,必须带http://或https://前缀,不要拼接接口路径,比如正确格式为https://api.target-service.com,漏写HTTPS前缀、拼错域名是最高发的错误
    • 若API使用非80/443的自定义端口,单独填写到Port字段,不要拼在Host中
    • 先清空Extra字段内的自定义代理、证书配置,做基础连通性测试
  • 验证Airflow运行环境的网络连通性
    不要在本地机器做测试,直接登录Airflow任务实际运行的节点(单机部署就是Airflow所在服务器,分布式部署是Worker节点),用curl发起和代码完全一致的POST请求:
    curl -X POST "你的API完整请求地址" \
    -H "Authorization: Bearer Hqasdsdhasdhsdqigd39318" \
    -H "Content-Type: application/json" \
    -d '替换为代码中完整的JSON请求体'
    
    如果curl命令也超时,属于服务器层面网络问题:要么是目标API的IP白名单未添加Airflow节点的出口IP,要么是节点出网防火墙、安全组拦截了对应域名/端口的请求,和Airflow逻辑、Token有效性无关。
  • 补全SimpleHttpOperator缺失参数
    当前代码未传入endpoint参数,SimpleHttpOperator会将连接配置中的Host与endpoint拼接为最终请求地址,缺失该参数时默认请求Host根路径,若实际接口存在子路径必然会请求失败。需根据实际API路径补全该参数,比如接口路径为/v1/claim/process则添加endpoint='/v1/claim/process'。
    同时可显式添加超时配置避免默认超时时间过短,自签名HTTPS场景可添加跳过证书校验参数:
    task_func = SimpleHttpOperator(
        task_id='process_source_claim_req',
        http_conn_id='source_api',
        method='POST',
        endpoint='/替换为实际接口路径',
        data=json.dumps(data),
        headers={"Authorization": "Bearer Hqasdsdhasdhsdqigd39318",
                  "Content-Type": "application/json"},
        timeout=30, # 按需调整超时时间,单位秒
        # verify=False, # 自签名证书场景取消注释
    )
    
  • 排查代理配置冲突
    若Airflow运行环境配置了全局HTTP/HTTPS代理,且目标API是内网地址,需要在source_api连接的Extra字段添加配置{"no_proxy": "目标API域名"},避免请求走公网代理绕转导致超时。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.26 13:15:37