sivaramanm

WinHTTP Send with Complex Send Parameters

7 posts in this topic

#1 ·  Posted (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 by sivaramanm

Share this post


Link to post
Share on other sites



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

 

Share this post


Link to post
Share on other sites

#4 ·  Posted (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 by sivaramanm

Share this post


Link to post
Share on other sites

@sivaramanm, no need to created a second thread on the same topic, so please stick to one topic going forward.

Merged.

Jos

1 person likes this

Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

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

 

Share this post


Link to post
Share on other sites

@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.

Share this post


Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.


Sign In Now

  • Similar Content

    • nhardel
      By nhardel
      I have been working on trying to develop some scripts to interface with the REST/JSON API from the Orion SDK.  This is where I will ask my questions and hopefully get some community responses that could help benefit others. 
      https://github.com/solarwinds/OrionSDK
      I am trying to create examples of how to interface with the API from autoit.  This should be a knowledge dump for this task. 
    • n3wbie
      By n3wbie
      How to send Requests on https Website
      I tried using
      ObjCreate("winhttp.winhttprequest.5.1")
      But m not Receiving Any response
      m able to retrive https://google.com but same is not available on other site( https://gst.gov.in )
      kindly help me
    • nhardel
      By nhardel
      So I have been bashing my head in for a couple days and have searched both AutoIT forums and Thwack Forums for an answer.  I understand this could be hard to help sense I can't provide a server for someone to help me test against.  I am trying to use the WinHTTP.au3 to connect with Solarwinds Orion SDK thru REST/JSON api calls.  Here is the documentation that they provide.
      https://github.com/solarwinds/OrionSDK/wiki/REST
      I have been trying just to make a basic connection but for some reason cannot get past the authorization process with WinHTTP.  Here is my test code.
      #Region Includes #include <log4a.au3> #include "WinHttp.au3" #EndRegion Global $sAddress = "https://usandl0213:17778/SolarWinds/InformationService/v3/Json/Query?query=SELECT+NodeID+FROM+Orion.NODES" Global $array_URL = _WinHttpCrackUrl($sAddress) ;~ Row|Col 0 ;~ [0]|https ;~ [1]|2 ;~ [2]|usandl0213 ;~ [3]|17778 ;~ [4]| ;~ [5]| ;~ [6]|/SolarWinds/InformationService/v3/Json/Query ;~ [7]|?query=SELECT+NodeID+FROM+Orion.NODES Global $hOpen = _winhttpOpen() If @error Then _log4a_Fatal("Error intializing the usage of WinHTTP functions") Exit 1 EndIf Global $hConnect = _winhttpConnect($hOpen, $array_URL[2]) If @error Then _log4a_Fatal("Error specifying the initial target server of an HTTP request.") _WinHttpCloseHandle($hOpen) Exit 2 EndIf Global $hRequest = _WinHttpOpenRequest($hConnect, _ "GET", _ "/SolarWinds/InformationService/v3/Json/Query?query=SELECT+NodeID+FROM+Orion.NODES", _ "HTTP/1.1") If @error Then _log4a_Fatal(MsgBox(48, "Error", "Error creating an HTTP request handle.") _WinHttpCloseHandle($hConnect) _WinHttpCloseHandle($hOpen) Exit 3 EndIf _WinHttpAddRequestHeaders($hRequest, "Authorization: Basic YXV0b2l0X2xvZ2luOnRlc3Q=") _WinHttpAddRequestHeaders($hRequest, "User-Agent: curl/7.20.0 (i386-pc-win32) libcurl/7.20.0 OpenSSL/0.9.8l zlib/1.2.3") _WinHttpAddRequestHeaders($hRequest, "Host: usandl0213:17778") _WinHttpAddRequestHeaders($hRequest, "Accept: */*") _WinHttpSendRequest($hRequest) If @error Then MsgBox(48, "Error", "Error sending specified request.") Close_request() Exit 4 EndIf ; Wait for the response _WinHttpReceiveResponse($hRequest) If @error Then MsgBox(48, "Error", "Error waiting for the response from the server.") Close_request() Exit 5 EndIf Global $sChunk, $sData ; See what's returned If _WinHttpQueryDataAvailable($hRequest) Then Global $sHeader = _WinHttpQueryHeaders($hRequest) ;~ ConsoleWrite(@crlf) ConsoleWrite($sHeader & @CRLF) ; Read While 1 $sChunk = _WinHttpReadData($hRequest) If @error Then ExitLoop $sData &= $sChunk WEnd ConsoleWrite($sData & @CRLF) ; print to console Else MsgBox(48, "Error", "Site is experiencing problems.") EndIf Close_request() Func Close_request() ; Close open handles and exit _WinHttpCloseHandle($hRequest) _WinHttpCloseHandle($hConnect) _WinHttpCloseHandle($hOpen) EndFunc I am definitely connecting to the server but get a 401 Unauthorized response.  Output of above script:
      Header:
      HTTP/1.1 401 Unauthorized Cache-Control: private Date: Thu, 27 Jul 2017 15:31:21 GMT Content-Length: 1668 Content-Type: text/html; charset=utf-8 Server: Microsoft-IIS/7.5 Set-Cookie: ASP.NET_SessionId=lgwin2qsbbrip2mxg01fot05; path=/; HttpOnly Set-Cookie: TestCookieSupport=Supported; path=/ Set-Cookie: Orion_IsSessionExp=TRUE; expires=Thu, 27-Jul-2017 17:31:21 GMT; path=/ WWW-Authenticate: Negotiate WWW-Authenticate: NTLM X-UA-Compatible: IE=9 X-AspNet-Version: 4.0.30319 X-Powered-By: ASP.NET X-Same-Domain: 1 X-Content-Type-Options: nosniff X-Frame-Options: SAMEORIGIN X-XSS-Protection: 1; mode=block Body:
      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head><link rel="stylesheet" type="text/css" href="/orion/js/jquery-1.7.1/jquery-ui.css.i18n.ashx?l=en-US&v=42660.90.L&csd=%23b0b9c5;%23d2ddec;%2392add1;" /> <link rel="stylesheet" type="text/css" href="/orion/styles/orionminreqs.css.i18n.ashx?l=en-US&v=42660.90.L&csd=%23b0b9c5;%23d2ddec;%2392add1;" /> <link rel="stylesheet" type="text/css" href="/webengine/resources/steelblue.css.i18n.ashx?l=en-US&v=42660.90.L&csd=%23b0b9c5;%23d2ddec;%2392add1;" /> <link rel="stylesheet" type="text/css" href="/orion/ipam/res/css/sw-events.css.i18n.ashx?l=en-US&v=42660.90.L&csd=%23b0b9c5;%23d2ddec;%2392add1;" /> <script type="text/javascript" src="/orion/js/orionminreqs.js.i18n.ashx?l=en-US&v=42660.90.L"></script> <script type="text/javascript" src="/orion/js/modernizr/modernizr-2.5.3.js.i18n.ashx?l=en-US&v=42660.90.L"></script> <script type="text/javascript" src="/orion/js/jquery-1.7.1/jquery-1.7.1.framework.min.js.i18n.ashx?l=en-US&v=42660.90.L"></script> <script type="text/javascript">(function(){var de=$(document.documentElement); de.addClass('sw-is-locale-en'); $.each(jQuery.browser,function(k,v){if(v===true){ de.addClass('sw-is-'+k); de.addClass('sw-is-'+k+'-'+parseInt(jQuery.browser.version)); }}); })();</script> <script type="text/javascript">SW.Core.Loader._cbLoaded('jquery');</script> <script type="text/javascript">SW.Core.Date._init(0,-14400000);</script> <title> </title></head> <body> <script> window.location = 'Login.aspx'; </script> </body> </html> To me this looks like it if it is still looking for my credentials.   I did verify that things work as expected using Chrome and REST test client.  I do get certificate errors in IE if I try to go directly.  Bypass certificate issues and page will try to save out to .json file
       
      Looking for any help.
    • baolo073
      By baolo073
      [ [ [ "Kinh Oanh,\r\n", "Dear Oanh,\r\n", null, null, 3 ], [ "C\u1ea3m \u01a1n b\u1ea1n r\u1ea5t nhi\u1ec1u v졢\u1ee9c th\u01b0 c\u1ee7a b\u1ea1n \u0111\u1ebfn m\u1ed9t v᩠ngṠtr\u01b0\u1edbc. ", "Thank you very much for your letter which arrived a few days ago.", null, null, 3 ], [ "Th\u1eadt \u0111⯧ y뵠khi nghe t\u1eeb b\u1ea1n.\r\n", "It was lovely to hear from you.\r\n", null, null, 3 ], [ "b\u1ea1n \u0111i \u0111㵠v\u1eady.\r\n", "where do you go.\r\n", null, null, 1 ], [ "T\u1ea1m bi\u1ec7t!", "Goodbye!", null, null, 1 ] ], null, "en", null, null, null, 0.91366601, null, [ [ "en" ], null, [ 0.91366601 ], [ "en" ] ] ] How to parse array to json?
    • NiftRex
      By NiftRex
      I'm trying to get an array from a website so that I can just get the url, but I am not sure how. I read a bit of arrays but I have a feeling I'd have to be writing a lot more than what I should be. I will include the script I have so far and the API url for what I want.
       
      API: https://api.fast.com/netflix/speedtest?https=true&token=YXNkZmFzZGxmbnNkYWZoYXNkZmhrYWxm&urlCount=1 (I want the 'url' array that contains the url)
       
      Code:
      #include <MsgBoxConstants.au3> #include <Inet.au3> #include <Array.au3> $site = _INetGetSource('http://api.fast.com/netflix/speedtest?https=true&token=YXNkZmFzZGxmbnNkYWZoYXNkZmhrYWxm&urlCount=1') MsgBox($MB_SYSTEMMODAL, "Title", $site[1])