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

Robotframework POST多对象请求报400错误,求技术解决方案

求助:Robotframework构造多对象POST请求时出现400错误

正在进行API自动化测试,需要构造Postman中的多对象POST请求(请求示例如下),尝试编写Robotframework代码构造请求后,发送时出现HTTPError: 400 Client Error错误,附上代码请求协助解决。

Postman请求示例

{
    "affordability": {
        "grossIncome": 100000,
        "netIncome": 80000,
        "capitalRequired": 25000,
        "groceries": 200,
        "utilities": 300,
        "savings": 400,
        "services": 500,
        "transport": 600,
        "support": 800,
        "housing": 700,
        "other": 900
    },
    "employment": {
        "sector": "CentralGovernmentSocial",
        "status": "Contract",
        "startDate": "2010-08-01",
        "endDate": "2030-08-01"
    },
    "declarations": {
        "debtLiability": "None",
        "pendingRetrenchment": "false",
        "knownMedicalCondition": "false"
    },
    "bank": "ABSA"
}

我的Robotframework代码

#    &{baseURL}=          this is a url
#    ${offer}=           /offer
#    ${URL}=             Catenate           This is a url          ${appID}              /offer
#    log to console      ${URL}
#    Create Session      httpbin             this is a url
    &{affordability}=      Create Dictionary    grossIncome=60000     netIncome=30000      capitalRequired=25000       groceries=500        utilities=400     savings=300     services=200     transport=100     support=100       housing=500      other=100
    ${affordabilityobject}=    Catenate     {'affordability':      ${affordability}
#    log to console          ${affordabilityobject}
    &{employment}=       Create Dictionary      sector=CentralGovernmentSocial         status=Contract        startDate=2019-08-01           endDate=2023-08-01
    ${employmentobject}=        Catenate     'employment':      ${employment}
#    log to console          ${employmentobject}
    &{declarations}=      Create Dictionary     debtLiability=None      pendingRetrenchment=false      knownMedicalCondition=false
    ${declarationsobject}=      Catenate     'declarations':      ${declarations}
#    log to console          ${declarationsobject}
#    &{bank}=              Create Dictionary     bank=FirstNationalBank
    ${bank}=              Catenate     'bank':'FirstNationalBank'}
    log to console      ${bank}
    ${fullrequest}=             Catenate        SEPARATOR=,        ${affordabilityobject}      ${employmentobject}       ${declarationsobject}     ${bank}
    log to console      ${fullrequest}
    ${resp}=    Post      this is a url/${appID}/offer         json=${fullrequest}

问题原因及修正方案

错误原因

你现在的代码用Catenate手动拼接字符串构造请求体,会生成格式非法的JSON:

  • 直接拼接Robotframework字典会保留框架自身的格式(比如{grossIncome:60000 ...}),不是标准JSON的键值对格式
  • 拼接过程缺少必要的括号、引号,整体结构混乱,接口无法解析请求体,因此返回400错误

修正后的代码

Robotframework的Post关键字的json参数可直接接收嵌套字典,框架会自动将其序列化为合法JSON,无需手动拼接:

# 定义各个子字典
&{affordability}=      Create Dictionary    grossIncome=60000     netIncome=30000      capitalRequired=25000       groceries=500        utilities=400     savings=300     services=200     transport=100     support=100       housing=500      other=100
&{employment}=         Create Dictionary    sector=CentralGovernmentSocial         status=Contract        startDate=2019-08-01           endDate=2023-08-01
&{declarations}=       Create Dictionary    debtLiability=None      pendingRetrenchment=false      knownMedicalCondition=false

# 构造顶层请求体字典,整合所有字段
&{full_request}=       Create Dictionary    affordability=${affordability}    employment=${employment}    declarations=${declarations}    bank=FirstNationalBank

# 发送POST请求,直接传入顶层字典即可
${resp}=               Post                this is a url/${appID}/offer         json=${full_request}
log to console         ${resp.json()}

关键说明

  1. 先创建所有子字典,再用一个顶层字典整合所有请求字段,结构和Postman示例完全对应
  2. Post关键字会自动处理字典到JSON的转换,生成接口可识别的合法请求体
  3. 可以通过${resp.json()}打印响应内容,方便后续排查问题

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.15 18:50:26