Skip to main content

post() Using Customized Headers

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 POST request to example.website.com creating a note using the REST endpoint createnote.

Sending a POST request with customized headers
function ExamplePostRequest(five: Five, context: any, result: FiveError) : FiveError {
five.log('ExamplePostRequest');
const webClient = five.httpClient();

////////////////////////////////////////////////////////////////////////////////////////////////
// You can add http headers
////////////////////////////////////////////////////////////////////////////////////////////////
webClient.addHeader('Authorization', 'Basic ZGVtbzpwQDU1dzByZA==');

////////////////////////////////////////////////////////////////////////////////////////////////
// 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('data1', 'value1');
webClient.addData('data2', 'value2');

////////////////////////////////////////////////////////////////////////////////////////////////
// We can post to a REST end point the above data, in the example above it will
// be a urlencoded form.
////////////////////////////////////////////////////////////////////////////////////////////////
const webResult = webClient.post('https://example.website.com/api/createnote');
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);
}