web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Small and medium business | Business Central, N...
Suggested Answer

Using Shared Key with Rest API to Put file to Blob Storage

(0) ShareShare
ReportReport
Posted on by 20

Hello Guys, 

Im currently trying to send a PUT Request to my Azure Blob Storage via AL. But im facing the problem setting up a working Request. Im receiving a 403 Error. 

I have already implemented this in c# for testing this in general. This works like a charm. 

{    procedure PutBlob(var TmpBlob: Codeunit "Temp Blob")
    var

        UtcDateTimeMgt: Codeunit "Type Helper";
        WebClient: HttpClient;
        WebResponse: HttpResponseMessage;
        Headers: HttpHeaders;
        Content: HttpContent;
        Uri: Text;
        InStr: InStream;
    begin
        UtcNow := UtcDateTimeMgt.GetCurrUTCDateTimeAsText();
        Uri := StrSubstNo('http://%1.blob.core.windows.net/%2/%3', GetStorageAccountName(), GetContainerName(), GetFutureBlobName());
      
        AddDefaultHeaderWithCheckUp(WebClient, 'x-ms-blob-type', 'Blockblob');
        AddDefaultHeaderWithCheckUp(WebClient, 'x-ms-date', UtcNow);
        AddDefaultHeaderWithCheckUp(WebClient, 'x-ms-version', GetApiVersion());
        TmpBlob.CreateInStream(InStr);

        Content.Clear();
        Content.WriteFrom(InStr);
        Headers.Clear();

        Content.GetHeaders(Headers);
        Headers.Remove('Content-Type');
        Headers.Add('Content-Type', FileMgt.GetFileNameMimeType(GetFutureBlobName()));
        Headers.Add('Content-Length', Format(TmpBlob.Length(), 0, 9));
        Content.GetHeaders(Headers);

        WebClient.DefaultRequestHeaders.Add('Authorization', GetAuthorizationHeader(TmpBlob));
        if WebClient.Put(Uri, Content, WebResponse) = false then
            Error(WebResponse.ReasonPhrase())
        else
            Message(StrSubstNo('Reason:%1', WebResponse.ReasonPhrase()));
    end;

    local procedure GetAuthorizationHeader(var TmpBlob: Codeunit "Temp Blob") AuthorizationHeader: Text
    var
        Hmac: Codeunit "Cryptography Management";
        HeaderResource: Text;
        UrlResource: Text;
        Signature: Text;
        HashAlgo: Option HMACMD5,HMACSHA1,HMACSHA256,HMACSHA384,HMACSHA512;
    begin
        NewLine[1] := 10;
        HeaderResource := 'x-ms-blob-type:BlockBlob'   NewLine   'x-ms-date:'   UtcNow   NewLine   'x-ms-version:'   GetApiVersion();
        UrlResource := '/'   GetStorageAccountName()   '/'   GetContainerName()   '/'   GetFutureBlobName();

        //Signature=Base64(HMAC-SHA256(UTF8(StringToSign), Base64.decode()))  


        Signature := 'PUT'   NewLine   NewLine   NewLine   Format(TmpBlob.Length(), 0, 9)   NewLine   NewLine   FileMgt.GetFileNameMimeType(GetFutureBlobName())  
                                                                NewLine   NewLine   NewLine   NewLine   NewLine   NewLine   NewLine   HeaderResource   NewLine   UrlResource;

        AuthorizationHeader := 'SharedKey '   GetStorageAccountName()   ':'   Hmac.GenerateBase64KeyedHashAsBase64String(Signature, GetStorageKey(), HashAlgo::HMACSHA256);
        //https://github.com/microsoft/ALAppExtensions/blob/master/Modules/System/Cryptography Management/README.md#generatehashasbase64string-method--1  
    end;

    local procedure AddDefaultHeaderWithCheckUp(Webclient: HttpClient; Name: Text; Value: Text)

    begin
        if not Webclient.DefaultRequestHeaders.Add(Name, Value) then
            Error((StrSubstNo('Error on %1', Name)));
    end;

I really dont know how to make this working since its similar to my C# Put Request.

I did some local procedures for example login data. (The GetObjectName() procedures). The API Version im using is 

  •     local procedure GetApiVersion() VersionText
        begin
            Version := '2014-02-14';
        end;

feel free to ask me If you have any Questions about my Code above.


Thanks in advance. :-) 

I have the same question (0)
  • Suggested answer
    Andy Sather Profile Picture
    on at

    Hello  - We currently do not have dedicated Dev support via the Dynamics 365 Business Central forums, but I wanted to provide you some additional resources to assist.  If you need assistance with debugging or coding I would recommend discussing this on one of our communities.

    www.yammer.com/dynamicsnavdev

    dynamicsuser.net/.../developers

    I will open this up to the community in case they have something to add.

  • Community Member Profile Picture
    on at

    The 403 Forbidden error is an HTTP status code which means that accessing the page or resource you were trying to reach is absolutely forbidden for some reason. This article contains basic troubleshooting instructions for how you can identify and resolve a 403 Forbidden error.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Small and medium business | Business Central, NAV, RMS

#1
OussamaSabbouh Profile Picture

OussamaSabbouh 2,066

#2
YUN ZHU Profile Picture

YUN ZHU 658 Super User 2025 Season 2

#3
Sumit Singh Profile Picture

Sumit Singh 595

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans