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