HttpClient.robot 4.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. *** Settings ***
  2. Library HttpLibrary.HTTP
  3. Library RequestsLibrary
  4. Library Collections
  5. Library DateTime
  6. Library CustomLibrary
  7. *** Keywords ***
  8. PostRequest
  9. [Arguments] ${host} ${path} ${datas} ${params}=${EMPTY} ${headers}=None ${cookies}=None ${timeout}=30
  10. #处理请求
  11. ${header_dict} Create Dictionary Content-Type=application/json
  12. Run Keyword If ${headers}==${None} Log 没有添加自定义header
  13. ... ELSE Run Keyword AddHeader ${headers} ${header_dict}
  14. #处理cookies
  15. ${cookie_dict} Create Dictionary
  16. Run Keyword If ${cookies}==${None} Log 没有添加cookie信息
  17. ... ELSE Run Keyword AddCookies ${cookies} ${cookie_dict}
  18. #创建session
  19. Create Session example_robotframework ${host} timeout=${timeout} cookies=${cookie_dict}
  20. #发起post请求
  21. ${resp} POST On Session example_robotframework ${path} json=${datas} headers=${header_dict} params=${params}
  22. [Return] ${resp}
  23. AddHeader
  24. [Arguments] ${dict1} ${dict2}
  25. [Documentation] *遍历字典变量dict,并将dict1中的值添加到dict2中*
  26. Log 在请求体中添加自定义header
  27. ${items} Get Dictionary Items ${dict1}
  28. FOR ${index} ${key} ${value} IN ENUMERATE @{items}
  29. Set To Dictionary ${dict2} ${key}=${value}
  30. AddCookies
  31. [Arguments] ${cookies} ${cookiedict}
  32. [Documentation] *处理cookie*
  33. @{cookielist}= Split String ${cookies} ;
  34. FOR ${cookie} IN @{cookielist} #用;分割cookie
  35. Run Keyword If '${cookie}'=='${None}' Exit For Loop #如果Cookie为None,则跳出循环体
  36. ${cookie_split}= Split String ${cookie} =
  37. Set To Dictionary ${cookiedict} ${cookie_split[0]}=${cookie_split[1]}
  38. PutRequest
  39. [Arguments] ${host} ${path} ${datas} ${params}=${EMPTY} ${headers}=None ${cookies}=None ${timeout}=30
  40. #处理请求
  41. ${header_dict} Create Dictionary Content-Type=application/json
  42. Run Keyword If ${headers}==${None} Log 没有添加自定义header
  43. ... ELSE Run Keyword AddHeader ${headers} ${header_dict}
  44. #处理cookies
  45. ${cookie_dict} Create Dictionary
  46. Run Keyword If ${cookies}==${None} Log 没有添加cookie信息
  47. ... ELSE Run Keyword AddCookies ${cookies} ${cookie_dict}
  48. #创建session
  49. Create Session example_robotframework ${host} timeout=${timeout} cookies=${cookie_dict}
  50. #发起put请求
  51. ${resp} PUT On Session example_robotframework ${path} json=${datas} headers=${header_dict} params=${params}
  52. [Return] ${resp}
  53. DeleteRequest
  54. [Arguments] ${host} ${path} ${params}=${EMPTY} ${headers}=None ${cookies}=None ${timeout}=30
  55. #处理请求
  56. ${header_dict} Create Dictionary Content-Type=application/json
  57. Run Keyword If ${headers}==${None} Log 没有添加自定义header
  58. ... ELSE Run Keyword AddHeader ${headers} ${header_dict}
  59. #处理cookies
  60. ${cookie_dict} Create Dictionary
  61. Run Keyword If ${cookies}==${None} Log 没有添加cookie信息
  62. ... ELSE Run Keyword AddCookies ${cookies} ${cookie_dict}
  63. #创建session
  64. Create Session example_robotframework ${host} timeout=${timeout} cookies=${cookie_dict}
  65. #发起delete请求
  66. ${resp} DELETE On Session example_robotframework ${path} headers=${header_dict} params=${params}
  67. [Return] ${resp}
  68. GetRequest
  69. [Arguments] ${host} ${path} ${datas} ${params} ${headers}=None ${cookies}=None ${timeout}=30
  70. #处理请求
  71. ${header_dict} Create Dictionary Content-Type=application/json
  72. Run Keyword If ${headers}==${None} Log 没有添加自定义header
  73. ... ELSE Run Keyword AddHeader ${headers} ${header_dict}
  74. #处理cookie
  75. ${cookie_dict} Create Dictionary
  76. Run Keyword If ${cookies}==${None} Log 没有添加cookie信息
  77. ... ELSE Run Keyword AddCookies ${cookies} ${cookie_dict}
  78. #创建session
  79. Create Session example_robotframework ${host} timeout=${timeout} cookies=${cookie_dict}
  80. #发起GET请求
  81. ${resp} RequestsLibrary.Get_Request example_robotframework ${path} headers=${header_dict} params=${params}
  82. [Return] ${resp}