get() Using Basic Authentication
Last updated 2/04/2024
The code is creating an instance of the HTTPClient
object using the function httpClient()
on the Five
object. The example will send a GET
request to example.website.com using basic
authentication.
Sending a GET request using basic authentication
function ExampleGetRequest(five: Five, context: any, result: FiveError) : FiveError {
five.log('ExampleGetRequest');
const webClient = five.httpClient();
////////////////////////////////////////////////////////////////////////////////////////////////
// Using setBasicAuthentication to set the user and password for this request
////////////////////////////////////////////////////////////////////////////////////////////////
webClient.setBasicAuthentication('simon', 'opensesame');
////////////////////////////////////////////////////////////////////////////////////////////////
// Setting the content type
////////////////////////////////////////////////////////////////////////////////////////////////
webClient.setContentType('application/x-www-form-urlencoded');
////////////////////////////////////////////////////////////////////////////////////////////////
// for form urlencoded data, you can us the addData helper
// otherwise you can set the content directly with webClient.setContent
////////////////////////////////////////////////////////////////////////////////////////////////
webClient.addData('category', 'clothing');
////////////////////////////////////////////////////////////////////////////////////////////////
// We can post to a REST end point the above data, in the example above it will
// be a urlencoded form.
////////////////////////////////////////////////////////////////////////////////////////////////
const webResult = webClient.get('https://example.website.com/api/notes');
if (webResult.isOk() === false) {
return five.createError(webResult);
}
////////////////////////////////////////////////////////////////////////////////////////////////
// webResult has a few properties that can be used to parse the response
// webResult.raw - original resonse, base64 encoded
// webResult.response - a JSON response
//
// below is an example dealing with raw response
////////////////////////////////////////////////////////////////////////////////////////////////
const decodeBase64 = function(s: string): string {
var e={},i,b=0,c,x,l=0,a,r='',w=String.fromCharCode,L=s.length;
var A="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
for(i=0;i<64;i++){e[A.charAt(i)]=i;}
for(x=0;x<L;x++){
c=e[s.charAt(x)];b=(b<<6)+c;l+=6;
while(l>=8){((a=(b>>>(l-=8))&0xff)||(x<(L-2)))&&(r+=w(a));}
}
return r;
};
const response = decodeBase64(webResult.raw);
////////////////////////////////////////////////////////////////////////////////////////////////
// now we can work with the response as needed.
// for demonstration purposes, we will just log it for interrogating in the five inspector
////////////////////////////////////////////////////////////////////////////////////////////////
five.log(response);
return five.success(result);
}
Sending a GET request using basic authentication
function ExampleGetRequest(five, context, result) {
five.log('ExampleGetRequest');
const webClient = five.httpClient();
////////////////////////////////////////////////////////////////////////////////////////////////
// Using setBasicAuthentication to set the user and password for this request
////////////////////////////////////////////////////////////////////////////////////////////////
webClient.setBasicAuthentication('simon', 'opensesame');
////////////////////////////////////////////////////////////////////////////////////////////////
// Setting the content type
////////////////////////////////////////////////////////////////////////////////////////////////
webClient.setContentType('application/x-www-form-urlencoded');
////////////////////////////////////////////////////////////////////////////////////////////////
// for form urlencoded data, you can us the addData helper
// otherwise you can set the content directly with webClient.setContent
////////////////////////////////////////////////////////////////////////////////////////////////
webClient.addData('category', 'clothing');
////////////////////////////////////////////////////////////////////////////////////////////////
// We can post to a REST end point the above data, in the example above it will
// be a urlencoded form.
////////////////////////////////////////////////////////////////////////////////////////////////
const webResult = webClient.get('https://example.website.com/api/notes');
if (webResult.isOk() === false) {
return five.createError(webResult);
}
////////////////////////////////////////////////////////////////////////////////////////////////
// webResult has a few properties that can be used to parse the response
// webResult.raw - original resonse, base64 encoded
// webResult.response - a JSON response
//
// below is an example dealing with raw response
////////////////////////////////////////////////////////////////////////////////////////////////
const decodeBase64 = function(s) {
var e={},i,b=0,c,x,l=0,a,r='',w=String.fromCharCode,L=s.length;
var A="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
for(i=0;i<64;i++){e[A.charAt(i)]=i;}
for(x=0;x<L;x++){
c=e[s.charAt(x)];b=(b<<6)+c;l+=6;
while(l>=8){((a=(b>>>(l-=8))&0xff)||(x<(L-2)))&&(r+=w(a));}
}
return r;
};
const response = decodeBase64(webResult.raw);
////////////////////////////////////////////////////////////////////////////////////////////////
// now we can work with the response as needed.
// for demonstration purposes, we will just log it for interrogating in the five inspector
////////////////////////////////////////////////////////////////////////////////////////////////
five.log(response);
return five.success(result);
}