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

Starlette API调用返回tuple而非JSON/响应对象问题求助

问题分析与解决:Starlette API前端获取到tuple类型结果

问题根源

你前端代码里的变量赋值末尾多了个逗号:

association = requests.get(
    "http://localhost:9999/get-association",
    headers=headers,
),  # 这里的逗号是核心问题!

在Python中,表达式末尾加逗号会自动把结果包装成元组(tuple),所以你打印的类型才是tuple,和后端返回的内容无关。

修复步骤

  1. 先去掉多余的逗号,获取正确的请求响应对象:
headers = {"Authorization": settings.API_KEY}
# 移除末尾的逗号
association_response = requests.get(
    "http://localhost:9999/get-association",
    headers=headers
)
print("响应对象类型:", type(association_response))  # 现在会是 <class 'requests.models.Response'>
  1. 如果要获取后端返回的JSON数据,调用响应对象的.json()方法解析:
association_data = association_response.json()
print("解析后的JSON数据类型:", type(association_data))  # 会是 <class 'dict'>
print("数据内容:", association_data)  # 输出 {'association': 'test data'}

后端确认

你的Starlette后端代码没有问题,返回的JSONResponse会正确输出JSON格式数据,前端修复后就能正常获取到预期的字典类型数据了。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.01 08:50:22