首页 > 解决方案 > 使用带有嵌套 subdictionary.RobotFrameowork 的字典向服务端点发布请求

问题描述

我有一个服务定义(Rest),我想在 Robot Frameowrk 中执行 POST Request lavereging RequestsLibrary 。问题在于服务本身,它被设计为接受带有子字典的字典。你觉得创建这样的 Post 方法是否可行?

服务定义接受以下结构。我正在创建以下关键字来编排:

PostRequestAPI
    Create Session    ${AliasName}    ${PostReqURL}   auth=${Authen}
    ${head}=    Create Dictionary    Content-Type=application/json        Authorization=Basic c3U6Z3c=
    ${resp}=    Post Request    ${AliasName}      ${PostReqURI}    data={"payload":{"BatchFeeAmount":10, "NonCollectableFeeAmount':11,"FileID":"234324324322"},"header":{"saveDate":"2019-07-17T16:37:22.123Z" }}  headers=${head}    # (edited) 
   ${resp} = Response [400] [Expected service definition ]

标签: python-requestsrobotframework

解决方案


有可能的。有2种方式:

Method 1:您有 2 个字典,payloadheader,您想在第三个字典中用作键值对。如果你一步一步走,下面是你最终会得到的代码:

Dictionary of dictionary 
    ${payload}  Create Dictionary  BatchFeeAmount=10  NonCollectableFeeAmount=11  FileID=234324324322  
    ${header}   Create Dictionary  saveDate=2019-07-17T16:37:22.123Z
    ${data}     Create Dictionary  payload=${payload}  header=${header}

    Log To Console  ${data}
    // ${data} contains the API POST request payload in your desired format 

Method 2:您还可以将嵌套字典定义为纯 Python 变量,然后在机器人脚本中使用该文件。如下所示:

变量.py

data ={"payload":{"BatchFeeAmount":10,  "NonCollectableFeeAmount":11,  "FileID":234324324322}, "header":{"saveDate":"2019-07-17T16:37:22.123Z"}}

test_case.robot

*** Settings ***
Variables  /path/to/variables.py

*** Test Cases ***
Dictionary of Dictionaries
    Log to Console    ${data}

推荐阅读