LtNeno Posted October 8, 2019 Share Posted October 8, 2019 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 More sharing options...
Danp2 Posted October 8, 2019 Share Posted October 8, 2019 Have you tried something like this? $sReturned = _WinHttpSimpleRequest($hConnect,"POST", Default, Default, StringToBinary($sjson_req, $_WD_BFORMAT)) Latest Webdriver UDF Release Webdriver Wiki FAQs Link to comment Share on other sites More sharing options...
LtNeno Posted October 8, 2019 Author Share Posted October 8, 2019 12 minutes ago, Danp2 said: Have you tried something like this? $sReturned = _WinHttpSimpleRequest($hConnect,"POST", Default, Default, StringToBinary($sjson_req, $_WD_BFORMAT)) I'm a bit confused: where am I supposed to get the $_WD_BFORMAT variable from? Link to comment Share on other sites More sharing options...
Danp2 Posted October 8, 2019 Share Posted October 8, 2019 @LtNeno Sorry, that's from the Webdriver UDF. Replace it with $SB_UTF8. Latest Webdriver UDF Release Webdriver Wiki FAQs Link to comment Share on other sites More sharing options...
LtNeno Posted October 9, 2019 Author Share Posted October 9, 2019 same result (code 0), I'm clueless... Link to comment Share on other sites More sharing options...
Danp2 Posted October 9, 2019 Share Posted October 9, 2019 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) Latest Webdriver UDF Release Webdriver Wiki FAQs Link to comment Share on other sites More sharing options...
TheXman Posted October 9, 2019 Share Posted October 9, 2019 (edited) 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. expandcollapse popuppost_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 February 23, 2023 by TheXman hudsonhock and Decibel 2 CryptoNG UDF: Cryptography API: Next Gen jq UDF: Powerful and Flexible JSON Processor | jqPlayground: An Interactive JSON Processor Xml2Json UDF: Transform XML to JSON | HttpApi UDF: HTTP Server API | Roku Remote: Example Script About Me How To Ask Good Questions On Technical And Scientific Forums (Detailed) | How to Ask Good Technical Questions (Brief) "Any fool can know. The point is to understand." -Albert Einstein "If you think you're a big fish, it's probably because you only swim in small ponds." ~TheXman Link to comment Share on other sites More sharing options...
LtNeno Posted October 9, 2019 Author Share Posted October 9, 2019 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 More sharing options...
Danp2 Posted October 9, 2019 Share Posted October 9, 2019 @LtNeno That constant is defined in StringConstants.au3, so you can include that at the beginning of the script to get rid of the warning. Latest Webdriver UDF Release Webdriver Wiki FAQs Link to comment Share on other sites More sharing options...
TheXman Posted October 9, 2019 Share Posted October 9, 2019 (edited) You're welcome. 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 October 9, 2019 by TheXman CryptoNG UDF: Cryptography API: Next Gen jq UDF: Powerful and Flexible JSON Processor | jqPlayground: An Interactive JSON Processor Xml2Json UDF: Transform XML to JSON | HttpApi UDF: HTTP Server API | Roku Remote: Example Script About Me How To Ask Good Questions On Technical And Scientific Forums (Detailed) | How to Ask Good Technical Questions (Brief) "Any fool can know. The point is to understand." -Albert Einstein "If you think you're a big fish, it's probably because you only swim in small ponds." ~TheXman 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