Sometimes, I got error message 'Microsoft.Dynamics.Nav.Runtime.NavHttpContent variable not initialized.'
I met an error message 'Microsoft.Dynamics.Nav.Runtime.NavHttpContent variable not initialized.' when calling our API through using AL's HttpRequestMessage. This error message only can be reproduced occasionally on 'https://businesscentral.dynamics.com/sandbox' environment.

I debugged my AL code and found out that this error occurred when get HTTP response's content. I'm sure that the API I called is working fine and same code run on our Docker on-primise Business Central cannot reproduce this error. Could anybody help me to resolve this?
My AL code for calling API as below:
action(Test)
{
ApplicationArea = All;
trigger OnAction()
var
strUrl: Text;
client: HttpClient;
content: HttpContent;
respContent: HttpContent;
reqHeaders: HttpHeaders;
request: HttpRequestMessage;
response: HttpResponseMessage;
responseJsonString: Text;
errorMsg: Text;
begin
strUrl := "Website URL" + '/api/token';
request.SetRequestUri(strUrl);
reqHeaders.Clear();
request.GetHeaders(reqHeaders);
request.Method('POST');
if not client.Send(request, response) then begin
responseJsonString := 'API request failed';
end;
if not response.IsSuccessStatusCode() then begin
if not response.Content().ReadAs(errorMsg) then
errorMsg := responseJsonString;
responseJsonString := ' Error Message: \\' + errorMsg + '\\' +
'Status code: ' + Format(response.HttpStatusCode()) + '\\' +
'Description: ' + response.ReasonPhrase();
end else begin
respContent := response.Content();
respContent.ReadAs(responseJsonString);
end;
Message(responseJsonString);
end;
}
Versions:
- AL Language: 3.0.145991
- Business Central: Online Sandbox version
@monchangg if
if not response.IsSuccessStatusCode() then begin
returns an error status code that corresponds to a failed request, it might not have a content. If you are accessing the Content, it might not be initialized. You should test your code with a mock endpoint and see how it handles different failure conditions.
As this is not related to the development environment, it might be better to ask this in one of these forums: Business Central Community mibuso forum Dynamics User Group