microsoft / AL

Home of the Dynamics 365 Business Central AL Language extension for Visual Studio Code. Used to track issues regarding the latest version of the AL compiler and developer tools available in the Visual Studio Code Marketplace or as part of the AL Developer Preview builds for Dynamics 365 Business Central.
MIT License
728 stars 241 forks source link

[Bug]: HttpContent can't be initialized using Clear() method - Microsoft.Dynamics.Nav.Runtime.NavHttpContent variable not initialised #7606

Closed obychyk closed 8 months ago

obychyk commented 9 months ago

Describe the issue

HttpContent can't be initialized using Clear() method. When trying to send a request to a server I use HttpContent to initialize HttpHeaders and body of the request. Here is the code snippet:

local procedure InitializeContent()
var
    Request: HttpRequestMessage;
    Headers: HttpHeaders;
begin
    if not Request.Content.GetHeaders(Headers) then begin
        Request.Content.Clear();
        Request.Content.GetHeaders(Headers);
    end;

    Headers.Remove('Content-Type');
    Headers.Add('Content-Type', 'application/json');
end;

Based on description of method Clear() here, the method would initialize HttpContent object to default values.

Sets the HttpContent object to a default value. The content contains an empty string and empty headers.

This code snippet works as expected in local container environment (both OnPrem and Sandbox), however, when deployed to SaaS version - line Request.Content.GetHeaders(Headers); (inside of the if statement) returns an error:

Microsoft.Dynamics.Nav.Runtime.NavHttpContent variable not initialised.

Expected behavior

Method HttpContent.Clear() would initialize a default object of HttpContent which can be later used for further processing.

Steps to reproduce

See the provided code snippet

Additional context

I was able to overcome the issue using the following code snippet:

local procedure InitializeContent()
var
    Request: HttpRequestMessage;
    Headers: HttpHeaders;
begin
    if not Request.Content.GetHeaders(Headers) then begin
        Request.Content.WriteFrom('');
        Request.Content.GetHeaders(Headers);
    end;

    Headers.Remove('Content-Type');
    Headers.Add('Content-Type', 'application/json');
end;

So instead of Content.Clear() I used Content.WriteFrom('')

obychyk commented 9 months ago

Versions of BC tested on:

SBalslev commented 8 months ago

I have raised the concern internally, so I cannot guarantee if or when it will be addressed.

SBalslev commented 8 months ago

I have raised the concern internally, so I cannot guarentee if or when it will be addressed.

/ Let's get the issue to a team who will fix and backport it, as this repository is only for issues related to the AL compiler in latest developer preview environment for Dynamics 365 Business Central. We suggest that you open a support case or file a bug in Collaborate, to ensure that all Business Central users benefit from your catch as soon as possible.

To open a support case, you can:

If you file the bug in Collaborate, remember to include steps to reproduce the issue, and the Business Central build number and country version you're using.