WinHTTP使用复杂的发送参数发送

时间:2016-08-19 19:51:16

标签: json rest http-post autoit winhttp

我有这个输入参数(复杂的输入列表),需要作为HTTP POST请求的一部分发送。我一直在寻找相当于封装以下数据的AutoIt,但不成功。

由于显而易见的输入不完整原因(无法发送 ip_list 和其他数组类型参数),下面给出的工作代码失败了400 - Bad Request

问题

  1. 如何封装$oHttp.Send()的下列给定数据?
  2. 如何封装数据类型,如:

    null - aligned_device_template 参数

    白色间隔值 - description

    空数组 - 主机名 device_groups

  3. 要转换的JSON格式的输入参数&在AutoIt中发送

    {
          "name": "SnmpSIM",
          "description": "EM7 device created by BDD test case",
          "credentials":"{{feature.credential.body.result_set[*].URI}}", 
          "organization": "/api/organization/0",
          "aligned_device_template": null,
          "aligned_collector": "{{feature.appliance_id[0].id[0]}}{{feature.appliance_id[0].id[1]}}",
          "discover_non_snmp": "1",
          "scan_ports": [
            "21",
            "22",
            "23",
            "25",
            "80"
          ],
          "ip_lists": [
            {
            "start_ip": "{{feature.json.ip}}",
            "end_ip": "{{feature.json.ip}}"
            }
          ],
          "dhcp_enabled": "0",
          "duplicate_protection": "1",
          "model_device": "1",
          "log_all": "1",
          "scan_all_ips": null,
          "port_scan_timeout": null,
          "initial_scan_level": null,
          "scan_throttle": null,
          "interface_inventory_timeout": "600000",
          "max_interface_inventory_count": "10000",
          "bypass_interface_inventory": "0",
          "hostnames": [],
          "device_groups": []
        }
    

    工作代码

    ; The data to be sent
    $sPD = 'name=SnmpSIM&description=EM7 device created by BDD test case&credentials=37&organization=/api/organization/0&aligned_device_template=null&aligned_collector=1&discover_non_snmp=1&dhcp_enabled=0&duplicate_protection=1&model_device=1&log_all=1&scan_all_ips= null&port_scan_timeout= null&initial_scan_level= null&scan_throttle= null&interface_inventory_timeout=600000&max_interface_inventory_count=10000&bypass_interface_inventory=0&hostnames=&device_groups=&scan_ports=21'
    
    ; Creating the object
    $oHTTP = ObjCreate("winhttp.winhttprequest.5.1")
    $oHTTP.Open("POST", "http://10.2.4.18/api/discovery_session", False)
    $oHTTP.SetCredentials("username","password",0)
    
    $oHTTP.SetRequestHeader("Content-Type", "application/em7-resource-uri")
    
    ; Performing the Request
    $oHTTP.Send($sPD)
    
    ; Download the body response if any, and get the server status response code.
    $oReceived = $oHTTP.ResponseText
    $oStatusCode = $oHTTP.Status
    
    If $oStatusCode <> 200 then
     MsgBox(4096, "Response code", $oStatusCode)
    EndIf
    
    ; Saves the body response regardless of the Response code
     $file = FileOpen("Received.html", 2) ; The value of 2 overwrites the file if it already exists
     FileWrite($file, $oReceived)
     FileClose($file)
    

    参考样本VBA代码

     Dim Items As New Collection
     Dim Item As Dictionary
     Dim Id As Long
    
     For Id = 1 To 2
         Set Item = New Dictionary
         Item("iditem") = Id
         Item("amount") = 1
         Items.Add Item
     Next Id
     Request.AddBodyParameter "id", 5633
     Request.AddBodyParameter "items", Items
    
    $oDictionary = ObjCreate("Scripting.Dictionary")
    $oDictionary.ADD("start_ip", "10.20.7.31")
    $oDictionary.ADD("end_ip", "10.20.7.33")
    

0 个答案:

没有答案
相关问题