Consume Rest API in Business Central Code with Basic Auth
Rest API Call With Basic Auth | Woo Commerce Rest API
So i was working on some requirement of pushing products directly form Microrosft Business Central to E-Commerce side i.e WOO-Commerce. so what we need is to consume the woo side API's into our AL code (BC SIDE) that requires the basic auth in case of https authentication.
Here is a glimpse of code for understanding or picking some approach for the others seeking something like that in Business Central.
action(UnixTimestamp)
{
ApplicationArea = All;
trigger OnAction()
var
client: HttpClient;
request: HttpRequestMessage;
response: HttpResponseMessage;
contentHeaders: HttpHeaders;
content: HttpContent;
responseText: Text;
uri: Text;
_queryObj: Text;
AuthString: Text;
TempBlob: Record TempBlob temporary;
UserName: Text;
password: Text;
jsontext: JsonObject;
JOrderNoToken: JsonToken;
begin
//End Point to Hit
uri := 'Your URL';
//Making JSON DATA TO POST _queryObj := '{"name": "test2908"}';
// Credentials for basic auth UserName := 'YOUR USER NAME';
password := 'YOUR PASSWORD';
AuthString := STRSUBSTNO('%1:%2', UserName, Password);
TempBlob.WriteTextLine(AuthString);
AuthString := TempBlob.ToBase64String();
AuthString := STRSUBSTNO('Basic %1', AuthString);
// Add the payload to the content
content.WriteFrom(_queryObj);
// Retrieve the contentHeaders associated with the content
content.GetHeaders(contentHeaders);
contentHeaders.Clear();
contentHeaders.Add('Content-Type', 'application/json');
client.DefaultRequestHeaders().Add('Authorization', AuthString);
// Assigning content to request.Content will actually create a copy of the content and assign it.
// After this line, modifying the content variable or its associated headers will not reflect in
// the content associated with the request message
request.Content := content;
request.SetRequestUri(uri);
request.Method := 'POST';
client.Send(request, response);
// Read the response content as json.
response.Content().ReadAs(responseText);
//Complete Output json into responsetext Message(responseText);
// Reading only name and his value from whole json jsontext.readfrom(responseText);
jsontext.Get('name', JOrderNoToken);
Message('Name is %1', JOrderNoToken.AsValue().AsCode());
end;
}
}
}
Comments
Post a Comment