sivaramanm Posted August 19, 2016 Share Posted August 19, 2016 (edited) Have this following input parameter(Complex list of inputs) that need to be SEND as part of HTTP POST Request. Have been searching forums to find an AutoIT equivalent to enapsulate the following data, but unsuccessful. Below given Working Code is failing with 400-Bad Request for obvious reason of incomplete input - unable to send ip_list and other array type parameters Questions: 1. How to encapsulate the below given data for $oHttp.Send() 2. How to enapsulate special data types like null - aligned_device_tempate parameter White sapced values - () and empty array? - hostname or device_groups Input Parameter in JSON format to be converted & sent in AutoIT compatible { "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": [] } Working Code ; 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) SAMPLE VBA CODE for Reference 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") Edited August 19, 2016 by sivaramanm Link to comment Share on other sites More sharing options...
sivaramanm Posted August 22, 2016 Author Share Posted August 22, 2016 Need some help sending the following JSON content. I have looked at the JSON UDFs available here but couldn't understand how to use them for my use. { "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": [] } Auto-IT Script: $JSONString = "" $oHTTP = ObjCreate("winhttp.winhttprequest.5.1") $oHTTP.Open("GET", "http://10.2.4.18/api/credential/snmp?limit=100", False) $oHTTP.SetCredentials("em7admin","em7admin",0) $oHTTP.SetRequestHeader("Content-Type", "application/em7-resource-uri") $oHTTP.Send($JSONString) ; **Need helpt to encapsulate the JSON String.** $oReceived = $oHTTP.ResponseText $oStatusCode = $oHTTP.Status If $oStatusCode == 200 then $file = FileOpen("Received.html", 2) ; The value of 2 overwrites the file if it already exists FileWrite($file, $oReceived) FileClose($file) EndIf Link to comment Share on other sites More sharing options...
Danp2 Posted August 22, 2016 Share Posted August 22, 2016 You may want to check out the WinHTTP UDF Latest Webdriver UDF Release Webdriver Wiki FAQs Link to comment Share on other sites More sharing options...
sivaramanm Posted August 22, 2016 Author Share Posted August 22, 2016 (edited) @Danp2 Thanks for your response. I have looked at WinHTTP UDF. my issue is i am not sure how to send the JSON content either as a variable or as a binary data file. for instance cURL utulity has the following option to send the JSON data as a binary file. I am looking for either to send the JSON data in serialised format like key1=value1&key2=value2 format (which i tried but not working as given below) or sending as binary data file. curl -v -H 'X-em7-beautify-response:1' -u 'username:password' "https://192.168.10.205/api/ticket/279/note" -H 'content-type:application/json' -- data-binary @new_note.json Below one fails with 400-Bad Request Error.(Most likely due to invalid JSON format) $sPD = '"{""name"":""SnmpSIM"",""description"":""EM7devicecreatedbyBDDtestcase"",""credentials"":""/api/credential/snmp/38"",""organization"":""/api/organization/0"",""aligned_device_template"":null,""aligned_collector"":""3"",""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"":[]}"' $oHTTP = ObjCreate("winhttp.winhttprequest.5.1") $oHTTP.Open("POST", "http://10.2.4.31/api/discovery_session_active", False) $oHTTP.SetCredentials("username","password",0) $oHTTP.SetRequestHeader("Content-Type", "application/json") $oHTTP.Send($sPD) $oReceived = $oHTTP.ResponseText $oStatusCode = $oHTTP.Status MsgBox(1,1,$oStatusCode) Exit If $oStatusCode == 200 then $file = FileOpen("Received.html", 2) ; The value of 2 overwrites the file if it already exists FileWrite($file, $oReceived) FileClose($file) Else MsgBox(1,1,$oStatusCode) EndIf Edited August 22, 2016 by sivaramanm Link to comment Share on other sites More sharing options...
Developers Jos Posted August 22, 2016 Developers Share Posted August 22, 2016 @sivaramanm, no need to created a second thread on the same topic, so please stick to one topic going forward. Merged. Jos sivaramanm 1 SciTE4AutoIt3 Full installer Download page - Beta files Read before posting How to post scriptsource Forum etiquette Forum Rules Live for the present, Dream of the future, Learn from the past. Link to comment Share on other sites More sharing options...
AutoBert Posted August 22, 2016 Share Posted August 22, 2016 try this $oHTTP.Send(StringToBinary($sPD,SB_UTF8)) Maybe you have to change SB_UTF8 to a other value: [optional] Changes how the string is stored as binary: $SB_ANSI (1) = string data is ANSI (default) $SB_UTF16LE (2) = string data is UTF16 Little Endian $SB_UTF16BE (3) = string data is UTF16 Big Endian $SB_UTF8 (4) = string data is UTF8 Link to comment Share on other sites More sharing options...
orbs Posted August 22, 2016 Share Posted August 22, 2016 @sivaramanm, you may try the following: 1) what's the deal with the double-double-quotes in your JSON string? replace each pair of double-quotes characters with a single double-quotes character. 2) i'm not at all certain you need the enclosing double-quotes around your entire JSON string. try to remove them. 3) you may need to escape the double-quotes inside the JSON string. something like this: $sPD = '"{\"name\":\"SnmpSIM\",\"description\":\"EM7devicecreatedbyBDDtestcase\",\"credentials\":\"/api/credential/snmp/38\",\"organization\":\"/api/organization/0\",\"aligned_device_template\":null,\"aligned_collector\":\"3\",\"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\":[]}"' this is after replacing each pair of double-double-quotes ("") with a backslash and a double=quotes (\"), but the enclosing double-quotes pair is maintained. i know for a fact that escaping the double-quotes inside the JSON string is required when passing it to an external utility, e.g. cURL. not sure if this is needed in a direct call. Signature - my forum contributions: Spoiler UDF: LFN - support for long file names (over 260 characters) InputImpose - impose valid characters in an input control TimeConvert - convert UTC to/from local time and/or reformat the string representation AMF - accept multiple files from Windows Explorer context menu DateDuration - literal description of the difference between given dates Apps: Touch - set the "modified" timestamp of a file to current time Show For Files - tray menu to show/hide files extensions, hidden & system files, and selection checkboxes SPDiff - Single-Pane Text Diff Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now