Robot Framework -007 RequestsLibrary 复杂json字符串发出http post request

Posted 我有目标

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Robot Framework -007 RequestsLibrary 复杂json字符串发出http post request相关的知识,希望对你有一定的参考价值。

研究了很长时间才成功。

假如有比较复杂的json串需要通过post提交:

{
    "links": [],
    "fields": [],
    "children": {
        "serverRuntimes": {
            "links": [],
            "fields": ["name"],
            "children": {
                "JMSRuntime": {
                    "links": [],
                    "fields": ["name", "healthState"],
                    "children": {
                        "JMSServers": {
                            "links": [],
                            "fields": ["name", "healthState"],
                            "children": {
                                "destinations": {
                                    "links": [],
                                    "fields": ["name", "state"]
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

其中的healthState还需要使用变量替换

 

则RF的代码如下:

*** Settings ***
Library    RequestsLibrary    
Library    Collections    
Library    json   

JMS-related-runtimes
    ${ss}    Set Variable    healthState    
    ${json_string}=    catenate
     ...    {
     ...      "links": [], "fields": [],
     ...      "children": {
     ...        "serverRuntimes": {
     ...          "links": [], "fields": [ "name" ],
     ...          "children": {
     ...            "JMSRuntime": {
     ...              "links": [], "fields": [ "name", "${ss}" ],
     ...              "children": {
     ...                "JMSServers": {
     ...                  "links": [], "fields": [ "name", "${ss}" ],
     ...                  "children": {
     ...                    "destinations": {
     ...                      "links": [], "fields": [ "name", "state" ]
     ...                    }
     ...                  }
     ...                }
     ...              }
     ...            }
     ...          }
     ...        }
     ...      }
     ...    }

    ${json}=             evaluate        json.loads(\'\'\'${json_string}\'\'\')    json
    
    ${data}    Set Variable    ${json}    
    ${uri}     Set Variable    /domainRuntime/search
    ${resp}    Post Request    TOMCAT    ${uri}    data=${data}
    Log    ${resp} 
    Should Be Equal As Strings    \'${resp.status_code}\'    \'200\' 

 

POST 的数据如下,json的数据在红框里。同时发现变量也被替换了。

 

 

同时推荐一个 json 格式化工具 的网址:  https://www.freeformatter.com/json-formatter.html

以上是关于Robot Framework -007 RequestsLibrary 复杂json字符串发出http post request的主要内容,如果未能解决你的问题,请参考以下文章

Robot Framework - 从带有参数的其他 *.robot 脚本调用 *.robot 脚本

Robot Framework重点

用Python写Robot Framework测试

mac环境下搭建Robot framework

robot framework数据库操作

Python3 Robot Framework测试框架