LeTran Posted April 18 Share Posted April 18 Hello all, I am a total newbie looking to get some information from our server using Rest API. I am able to login using a browser and Postman app via "Basic Authentication", Ultimately I want to be able to connect using Auto so I can manipulate some data. This is what I have so far. I'm not getting any errors when running the code, but I'm also not getting anything for $data. Am I suppose to decode $data further? I'm expecting something like this to be returned. Any help is appreciated. Thanks. expandcollapse popup{ "meta": { "contentType": "RootDirectory" }, "content": { "_type": "RootDirectory", "externalVersion": "\"abas ERP 21\"", "internalVersion": "1.14.0", "protocolVersion": "50.1.0", "apidocs": { "help": [ { "description": "RESTful API Documentation", "rel": "help", "href": "../apidoc/index.html" }, { "description": "Swagger UI", "rel": "help", "href": "../swagger-ui.html" } ], "urn:abas:rel:child": [ { "description": "Open API 3.0 (json)", "rel": "urn:abas:rel:child", "href": "../v3/api-docs" } ] }, "clients": { "urn:abas:rel:client-fin": [ { "description": "fin", "rel": "urn:abas:rel:client-fin", "href": "/mw/r/fin", "objectType": "LinkList" } ], "urn:abas:rel:client-demo": [ { "description": "demo", "rel": "urn:abas:rel:client-demo", "href": "/mw/r/demo", "objectType": "LinkList" } ], "urn:abas:rel:client-erp": [ { "description": "erp", "rel": "urn:abas:rel:client-erp", "href": "/mw/r/erp", "objectType": "LinkList" } ], "urn:abas:rel:client-data": [ { "description": "data", "rel": "urn:abas:rel:client-data", "href": "/mw/r/data", "objectType": "LinkList" } ] } }, "protocolVersion": "50.1.0" } #include ".\json\Json.au3" $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 $hrequest = .Open("GET", "http://abas:10000/mw/r/", False) ;this is a local server on the network not accessible to the public. .SetRequestHeader("Content-Type", "application/json") .SetRequestHeader('Authorization', 'Basic ' & "*****:*****") .Send($hrequest) If @error Then ConsoleWrite("Error" & @CRLF) EndIf $iHttpStatus = .Status $sResponse = .ResponseText If $iHttpStatus <> 200 Then ConsoleWrite("HTTP Status : " & String($iHttpStatus) & @CRLF) ConsoleWrite("HTTP Response: " & @CRLF & $sResponse & @CRLF) EndIf EndWith $data = json_decode($sResponse) ConsoleWrite($data) Link to comment Share on other sites More sharing options...
Developers Jos Posted April 18 Developers Share Posted April 18 Moved to the appropriate AutoIt General Help and Support forum, as the Developer General Discussion forum very clearly states: Quote General development and scripting discussions. Do not create AutoIt-related topics here, use the AutoIt General Help and Support or AutoIt Technical Discussion forums. Moderation Team 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...
LeTran Posted April 18 Author Share Posted April 18 #include ".\json\Json.au3" $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("GET", "http://abas:10000/mw/r/", False) .SetRequestHeader("Content-Type", "application/json") .SetRequestHeader('Authorization', 'Basic ' & "*****:*****") .Send() If @error Then ConsoleWrite("Error" & @CRLF) EndIf $iHttpStatus = .Status $sResponse = .ResponseText If $iHttpStatus <> 200 Then ConsoleWrite("HTTP Status : " & String($iHttpStatus) & @CRLF) ConsoleWrite("HTTP Response: " & @CRLF & $sResponse & @CRLF) EndIf EndWith $data = json_decode($sResponse) ConsoleWrite($data) I modified the code a bit after realizing .Open doesn't returning anything. No error, $data still shows nothing. Link to comment Share on other sites More sharing options...
Nine Posted April 18 Share Posted April 18 Is there anything in $sResponse ? Have you tried to set user-agent in the header before the send ? I guess my crystal ball stops here.... “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
LeTran Posted April 18 Author Share Posted April 18 47 minutes ago, Nine said: Is there anything in $sResponse ? Have you tried to set user-agent in the header before the send ? I guess my crystal ball stops here.... Thanks Nine for your quick answer. I don't know why I didn't check for that. $sResponse did give me the correct data. Now I need to figure out a way to clean this up, or find a UDF that can decipher this. Do you know anything off hand? {"meta":{"contentType":"RootDirectory"},"content":{"_type":"RootDirectory","externalVersion":"\"abas ERP 21\"","internalVersion":"1.14.0","protocolVersion":"50.1.0","apidocs":{"help":[{"description":"RESTful API Documentation","rel":"help","href":"../apidoc/index.html"},{"description":"Swagger UI","rel":"help","href":"../swagger-ui.html"}],"urn:abas:rel:child":[{"description":"Open API 3.0 (json)","rel":"urn:abas:rel:child","href":"../v3/api-docs"}]},"clients":{"urn:abas:rel:client-fin":[{"description":"fin","rel":"urn:abas:rel:client-fin","href":"/mw/r/fin","objectType":"LinkList"}],"urn:abas:rel:client-demo":[{"description":"demo","rel":"urn:abas:rel:client-demo","href":"/mw/r/demo","objectType":"LinkList"}],"urn:abas:rel:client-erp":[{"description":"erp","rel":"urn:abas:rel:client-erp","href":"/mw/r/erp","objectType":"LinkList"}],"urn:abas:rel:client-data":[{"description":"data","rel":"urn:abas:rel:client-data","href":"/mw/r/data","objectType":"LinkList"}]}},"protocolVersion":"50.1.0"} Link to comment Share on other sites More sharing options...
Nine Posted April 18 Share Posted April 18 Depends what you want achieve in the end. In any case you already have the right UDF. Try this : Json_Dump($sResponse) “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy 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