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

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