Tech Articles


How to use HTTPClient or RESTClient to call an API with basic authentication


Background

Almost every API requires some sort of authentication. Basic authentication is the simplest way to handle authentication. Here we are going to do a simple example to show you how to use HTTPClient or RESTClient to call an API with basic authentication.

Access a simple API with basic authentication in IE

Let’s take a simple basic authentication API from the Internet as an example.

If we input https://jigsaw.w3.org/HTTP/Basic/ in IE and press Enter, then input Username: guest; Password: guest in the pop-up login window.

Click the Sign in button and you will get a response of the current IE accessing the API: “Your browser made it!”

Use the HTTPClient object to call an API with basic authentication

Here is the debug view:

Here is the sample PB code:

String 		ls_Basic, ls_UserName, ls_Password, ls_Body
Integer         li_Return
CoderObject 	lco_Code
HttpClient 	lhc_Client

lco_Code = Create CoderObject
lhc_Client = Create HttpClient

ls_UserName = "guest"
ls_Password = "guest"
ls_Basic = lco_code.base64encode( Blob(ls_UserName + ":" + ls_Password , EncodingUTF8!))

lhc_Client.SetRequestHeader( "Authorization", "Basic " + ls_Basic)
li_Return = lhc_Client.SendRequest("GET", "https://jigsaw.w3.org/HTTP/Basic/")
If li_Return = 1 And lhc_Client.GetResponseStatusCode() = 200 Then
	lhc_Client.GetResponseBody(ls_Body)
	MessageBox ("Tips" ,ls_Body )
End If

Destroy ( lco_Code )
Destroy ( lhc_Client )

Use the RESTClient object to call an API with basic authentication

Here is the debug view:

 

Here is the sample PB code:

String		 ls_Basic, ls_UserName, ls_Password, ls_Body
Integer          li_Return
CoderObject 	 lco_Code
RestClient 	 lhc_Client

lco_Code = Create CoderObject
lhc_Client = Create RestClient

ls_UserName = "guest"
ls_Password = "guest"
ls_Basic = lco_code.base64encode( Blob(ls_UserName + ":" + ls_Password , EncodingUTF8!))
lhc_Client.SetRequestHeader( "Authorization", "Basic " + ls_Basic)
li_Return = lhc_Client.sendgetrequest( "https://jigsaw.w3.org/HTTP/Basic/",ls_Body)
If li_Return = 1 And lhc_Client.GetResponseStatusCode() = 200  Then
	MessageBox ("Tips" ,ls_Body )
End If

Destroy ( lco_Code )
Destroy ( lhc_Client )
Comments (0)
There are no comments posted here yet

Find Articles by Tag

Database Table Schema PowerBuilder File IDE Syntax TFS Charts Data Branch & Merge Import JSON Encryption NativePDF SQL Server SqlModelMapper Visual Studio Debugging PowerServer Mobile DLL 64-bit CrypterObject DataWindow JSON Transaction JSONGenerator MessageBox Import PostgreSQL ODBC driver Model PBVM Script Messagging Web API Graph PDF Resize WebBrowser SnapDevelop .NET Assembly Stored Procedure PBDOM PFC Text DataWindow iOS CoderObject TreeView RibbonBar SOAP Platform Design UI Themes OAuth 2.0 License Automated Testing Mobile Debugger PowerBuilder (Appeon) SQL Jenkins Repository JSON Array InfoMaker Bug Migration Git Event Database Connection Variable XML PostgreSQL PowerBuilder Compiler Validation Service UI Modernization Debug Windows OS External Functions SVN API REST Linux OS C# HTTPClient Trial Outlook Installation Source Code RichTextEdit Control JSONParser Export Event Handler UI SDK Database Elevate Conference Authentication Authorization Event Handling WinAPI PowerScript (PS) Excel Oracle Sort CI/CD Web Service Proxy SnapObjects RESTClient Export JSON Database Painter COM TLS/SSL Source Control Application PDFlib Android Deployment .NET DataStore OLE DragDrop TortoiseGit OAuth Class ActiveX Performance Database Profile Database Table Data PowerServer Web Configuration Interface Testing DataType Error .NET Std Framework Window Filter Database Table ODBC Menu Open Source Database Object SqlExecutor Icon GhostScript BLOB Windows 10 Icons Expression Azure RibbonBar Builder OrcaScript DevOps PBNI Encoding 32-bit