Jump to content

Call to JSON REST API


 Share

Recommended Posts

Hi,

I'll be using autoit for some time to perform some query and interact with a SW I deliver using COM objects.

Now the SW I use changed its architecture and interaction must be accomplished via webapi.

I'm trying to use WinHttp to perform a call I tested in swagger, this is the curl:

 

curl -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' -d '{ \ 
   "username": "admin", \ 
   "password": "123", \ 
   "clientId": "AUTOIT_TEST", \ 
   "clientSecret": "E5AE290AB76A4B1E" \ 
 }' 'http://localhost/ARXivarNextWebApi/api/Authentication'

And this is my address:

http://localhost/ARXivarNextWebApi/api/Authentication

I know I'm missing some basic stuff because I tried to do this

#include <String.au3>
#include <File.au3>
#include <array.au3>
#include<_sql.au3>
#include<_scrivilog.au3>
#include<WinHttp.au3>
#include<WinHttpConstants.au3>

$sAddress = 'http://localhost/ARXivarNextWebApi/api/Authentication'
$sjson_req = '{ "username": "admin","password": "123","clientId": "AUTOIT_TEST","clientSecret": "E5AE290AB76A4B1E"}'

$hOpen = _WinHttpOpen()
MsgBox(0,"",$hOpen)

$hConnect = _WinHttpConnect($hOpen, $sAddress)
MsgBox(0,"",$hConnect)

$sReturned = _WinHttpSimpleSSLRequest($hConnect,"POST",'Content-Type: application/json','Accept: application/json',$sjson_req)

_WinHttpCloseHandle($hConnect)
_WinHttpCloseHandle($hOpen)

MsgBox(0,"TEST",$sReturned)

But I get error code 0: what am I missing?

Link to comment
Share on other sites

4 hours ago, LtNeno said:

same result (code 0), I'm clueless...

Try running the following and then post the console window output for our review --

#include<WinHttp.au3>
#include<WinHttpConstants.au3>

$sAddress = 'http://localhost/ARXivarNextWebApi/api/Authentication'
$sjson_req = '{ "username": "admin","password": "123","clientId": "AUTOIT_TEST","clientSecret": "E5AE290AB76A4B1E"}'

$aURL = _WinHttpCrackUrl($sAddress)

$hOpen = _WinHttpOpen()
$hConnect = _WinHttpConnect($hOpen, $aURL[2], $aURL[3])
ConsoleWrite("> _WinHttpConnect - " & @error & @crlf)

$sReturned = _WinHttpSimpleRequest($hConnect, "POST", $aURL[6], Default, StringToBinary($sjson_req, $SB_UTF8))
ConsoleWrite("> _WinHttpSimpleRequest - " & @error & @crlf)

_WinHttpCloseHandle($hConnect)
_WinHttpCloseHandle($hOpen)

ConsoleWrite("> Result - " & $sReturned & @crlf)

 

Link to comment
Share on other sites

The example below should work also.  It's a bit longer because it has comments, error checking, and several ConsoleWrites to see the progress, response, and any errors that may occur.  Since you were using COM before, the syntax below should be familiar to you.

 

post_authentication_test()

Func post_authentication_test()
    Local $oHttp   = Null, _
          $oComErr = Null

    Local $iHttpStatus = 0

    Local $sResponse = "", _
          $sPostData = ""


    ConsoleWrite(@CRLF & "Executing API" & @CRLF)

    ;Set COM error handler
    $oComErr = ObjEvent("AutoIT.Error", "com_error_handler")

    ;Create a HTTP COM object
    $oHttp = ObjCreate("winhttp.winhttprequest.5.1")
    If @error Then
        ConsoleWrite("Unable to create http request object." & @CRLF)
        Exit -1
    EndIf
    ConsoleWrite("WinHttpRequest object created." & @CRLF)

    With $oHttp
        ;Open POST request
        .Open("POST", "http://localhost/ARXivarNextWebApi/api/Authentication", False)

        ;Set request headers and options
        .SetRequestHeader("Content-Type", "application/json")

        ;Send request
        .Send('{"username":"admin","password":"123","clientId":"AUTOIT_TEST","clientSecret":"E5AE290AB76A4B1E"}')
        If @error Then
            ConsoleWrite(StringFormat("SEND ERROR: (0x%X) %s", $oComErr.Number, $oComErr.Description) & @CRLF)
            Return
        EndIf

        ;Get status code and response
        $iHttpStatus = .Status
        $sResponse   = .ResponseText

        ;If status code isn't okay
        If $iHttpStatus <> 200 Then
            ConsoleWrite("HTTP Status  : " & String($iHttpStatus) & @CRLF)
            ConsoleWrite("HTTP Response: " & @CRLF & $sResponse & @CRLF)
            Return
        EndIf
    EndWith

    ConsoleWrite("API Response:" & @CRLF & $sResponse & @CRLF)
EndFunc

Func com_error_handler($oError)
    #forceref $oError
    Return
EndFunc

 

Edited by TheXman
Link to comment
Share on other sites

Danp2: it keeps giving me a warning: $SB_UTF8: possibly used before declaration.

TheXman: It works great! I only had to purge the latest Func 'cause it seemed not recognized as function for some reason.

Thank you both very very much, now I only have to figure out how to deal with the response and so on but I'm positive!

Link to comment
Share on other sites

You're welcome.  :thumbsup:

 

40 minutes ago, LtNeno said:

I only had to purge the latest Func 'cause it seemed not recognized as function for some reason.

If by the "latest" function you mean the com_error_handler() function, then you need to figure out why you got the error because it is needed in case there is a COM error.  Although it just does a return, it allows the calling function to process @error if the COM call fails.

I'm glad that you have gotten past the hurdle of being able to successfully make the API call.  If you run into more obstacles, just request additional assistance that includes a detailed description of the problem, the expected results, and most importantly, the code that isn't working.

Edited by TheXman
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...