procedure CallAPI()
var
Content: HttpContent;
ContentHeaders: HttpHeaders;
IsSuccessful: Boolean;
HttpClient: HttpClient;
ResponseMessage: HttpResponseMessage;
JToken: JsonToken;
JObject: JsonObject;
JValue: JsonValue;
RequestHeaders: HttpHeaders;
RequestMessage: HttpRequestMessage;
ResponseText: Text;
begin
// HttpClient.UseWindowsAuthentication('username', 'password');
Content.GetHeaders(contentHeaders);
RequestMessage.GetHeaders(RequestHeaders);
RequestHeaders.Add('Username', 'username');
RequestHeaders.Add('Password', 'password');
// ContentHeaders.Add('Username', 'username');
// ContentHeaders.Add('Password', 'password');
// RequestHeaders.Add('request-id', Format(Random(9999)));
// RequestHeaders.Add('OData-Version', '4.0');
// RequestHeaders.Add('Access-Control-Allow-Credentials', 'true');
// RequestMessage.SetRequestUri(Rec./API URL/);
RequestMessage.SetRequestUri('http://localhost:7048/BC220/ODataV4/Company(''CRONUS%20India%20Ltd.'')/CustomAPI');
RequestMessage.Method('GET');
IsSuccessful := HttpClient.Send(RequestMessage, ResponseMessage);
if not IsSuccessful then
Error('Status Code: %1// Reason : %2', ResponseMessage.HttpStatusCode, ResponseMessage.ReasonPhrase)
else begin
ResponseMessage.Content.ReadAs(ResponseText);
JObject.ReadFrom(ResponseText);
Message(ResponseText);
end;
end;
Another Code :Here's revised code :procedure CallAPI()
var
RequestText: Text;
AuthText: Text;
Base64Text: Text;
Convert: Codeunit /Base64 Convert/;
Content: HttpContent;
ContentHeaders: HttpHeaders;
IsSuccessful: Boolean;
HttpClient: HttpClient;
ResponseMessage: HttpResponseMessage;
JToken: JsonToken;
JObject: JsonObject;
JValue: JsonValue;
RequestHeaders: HttpHeaders;
RequestMessage: HttpRequestMessage;
ResponseText: Text;
TempBlob: Codeunit /Temp Blob/;
begin
Content.WriteFrom(RequestText);
Content.GetHeaders(ContentHeaders);
RequestMessage.Content := Content;
RequestMessage.GetHeaders(RequestHeaders);
RequestMessage.SetRequestUri('http://localhost:7048/BC220/ODataV4/Company(''CRONUS%20India%20Ltd.'')/CustomAPI');
RequestMessage.Method('GET');
AuthText := strsubstNo('%1:%2', 'username', 'password');
Base64Text := Convert.ToBase64(AuthText); // Convert text to Base64
RequestHeaders.Add('Authorization', StrSubstNo('Basic %1', Base64Text));
RequestHeaders.Add('Connection', 'keep-alive');
HttpClient.Timeout := 30000;
HttpClient.send(RequestMessage, ResponseMessage);
end;