码迷,mamicode.com
首页 > Web开发 > 详细

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

时间:2020-05-14 10:28:15      阅读:68      评论:0      收藏:0      [点我收藏+]

标签:robot   att   mamicode   orm   html   sts   工具   代码   alt   

研究了很长时间才成功。

假如有比较复杂的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   att   mamicode   orm   html   sts   工具   代码   alt   

原文地址:https://www.cnblogs.com/abc789/p/12886614.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!