Jump to content

WinHttp Post _WinHttpSimpleSSLRequest


Recommended Posts

Hi Everyone,

I am struggling with the syntax using a post in _WinHttpSimpleSSLRequest, I am getting an @error exit.

I am also unsure of the syntax $sdta should be in.

 Here is a link to the API post with several examples.

https://docs.umbrella.com/destination-api/reference#post_v1-organizations-organizationid-destinationlists-destinationlistid-destinations

Here is the code I have so far, I have a successfull GET working using the same code but with "Global $hRequest = _WinHttpSimpleSendSSLRequest($hConnect, "GET", $Page) "

Thanks for any help :)

#include "WinHttp.au3"
#include <FileConstants.au3>


Global $Domain = "https://management.api.umbrella.com/"
Global $Page = "v1/organizations/1234567/destinationlists/7654321/destinations"

Global $hOpen = _winhttpOpen()
If @error Then
    Exit 1
EndIf

Global $hConnect = _winhttpConnect($hOpen, $Domain)
If @error Then
    _WinHttpCloseHandle($hOpen)
    Exit 2
EndIf

$sDta = "[{\"destination\":\"testing1.com\",\"comment\":\"testing1.com COMMENT\"},{\"destination\":\"testing2.com\",\"comment\":\"testing2.com COMMENT\"}]";
$sHeader = 'Content-Type: application/json'

Global $hRequest = _WinHttpSimpleSSLRequest($hConnect, "POST", $Page, Default, $sDta, $sHeader, True, 1)
_WinHttpAddRequestHeaders($hConnect, "Accept: application/json")
_WinHttpAddRequestHeaders($hRequest, "Authorization: Basic Key")

; Syntax.........: _WinHttpSimpleSSLRequest($hConnect [, $sType [, $sPath [, $sReferrer = Default [, $sDta = Default [, $sHeader = Default [, $fGetHeaders = Default [, $iMode = Default ]]]]]]])
; Parameters ....: $hConnect  - Handle from _WinHttpConnect
;                  $sType       - [optional] GET or POST (default: GET)
;                  $sPath       - [optional] request path (default: "" - empty string; meaning 'default' page on the server)
;                  $sReferrer   - [optional] referrer (default: $WINHTTP_NO_REFERER)
;                  $sDta        - [optional] POST-Data (default: $WINHTTP_NO_REQUEST_DATA)
;                  $sHeader     - [optional] additional Headers (default: $WINHTTP_NO_ADDITIONAL_HEADERS)
;                  $fGetHeaders - [optional] return response headers (default: False)
;                  $iMode       - [optional] reading mode of result
;                  |0 - ASCII-text
;                  |1 - UTF-8 text
;                  |2 - binary data



If @error Then
    _WinHttpCloseHandle($hConnect)
    _WinHttpCloseHandle($hOpen)
    Exit 3
EndIf

;Send the request
_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($sHeader & @CRLF);Show the header in console
    ;MsgBox(4096, "Header", $sHeader);Show the header in MsgBox
    ; Read
    While 1
        $sChunk = _WinHttpReadData($hRequest)
        If @error Then ExitLoop
        $sData &= $sChunk
    WEnd
    ;ConsoleWrite($sData & @CRLF) ;Show the data in console
    ;MsgBox(4096, "Data", $sData) ;Show the data in MsgBox
    MsgBox(4096, "Data", $sHeader & $sData) ;Show the data in MsgBox
Else
    MsgBox(48, "Error", "Site is experiencing problems.")
EndIf
 Close_request()

;Create path the log file
$sFilePath = (@ScriptDir & "\WriteResults.json")

;Open log file with overwrite flag
Local $hFile = FileOpen($sFilePath, $FO_OVERWRITE )

;Write date to log file
FileWrite($hFile, $sData)

;Close the log file
FileClose($hFile)

Func Close_request()
    ; Close open handles and exit
    _WinHttpCloseHandle($hRequest)
    _WinHttpCloseHandle($hConnect)
    _WinHttpCloseHandle($hOpen)
EndFunc

 

 

 

 

 

 

 

Link to post
Share on other sites
  • 4 weeks later...

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
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...