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

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