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

Python-Robot Framework API自动化报错:AttributeError: 'list' object has no attribute 'items'

问题排查:Robot Framework API测试报错AttributeError: 'list' object has no attribute 'items'

报错原因

你的代码中headers=@{header}传递的是列表类型变量,但RequestsLibrary的POST On Session关键字要求headers参数必须是字典类型,列表不存在items()方法,因此触发该错误。

修复方案

将headers的定义从列表改为字典即可解决问题:

1. 修改变量定义

把原来的列表变量@{header}替换为字典变量${header},两种写法任选其一:

# 写法1:用Create Dictionary关键字定义
*** Variables ***
${base_url}     https://reqres.in
${header}       Create Dictionary    Content-Type=application/json; charset=utf-8

# 写法2:直接定义字典字符串
${header}    {"Content-Type": "application/json; charset=utf-8"}

2. 调整测试用例中的调用

将headers=@{header}改为headers=${header}:

${response}=   POST On Session         mysession      api/users   data=${body}   headers=${header}

完整修复后的代码

*** Settings ***
Library    RequestsLibrary
Library    JSONLibrary
Library    Collections

*** Variables ***
${base_url}     https://reqres.in
${header}       Create Dictionary    Content-Type=application/json; charset=utf-8

*** Test Cases ***
POST_userInfo
    Create Session    mysession     ${base_url}
    ${body}=    Create Dictionary   name=Prad    job=leader

    ${response}=   POST On Session         mysession      api/users   data=${body}   headers=${header}
    Log To Console    ${response.status_code}
    Log To Console     ${response.content}
    Should Be Equal As Strings       ${response.status_code}  201

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.07 03:15:46