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

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