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