Jump to content

Rest API Basic Authentication - (Moved)


Recommended Posts

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.

{
    "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

  • Developers

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

#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

Link to comment
Share on other sites

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

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