Skip to main content

Notifications

Announcements

No record found.

Business Central forum

BC Webservice calling from AL

Posted on by 10
Hei,
I am having following code in converting C/AL to AL. This is giving me 401 unauthorized error.
Content.WriteFrom(RequestText);

WebRequest.SetRequestUri('http://localhost:7047/BC130/WS/CRONUS%20Norge%20AS/Codeunit/GetReservation');

WebRequest.Method('POST');
WebRequest.GetHeaders(RequestHeaders);
RequestText := strsubstNo('%1:%2', 'username', 'password');
TempBlob.WriteAsText(RequestText, TextEncoding::Windows);
RequestHeaders.Add('Authorization', StrSubstNo('Basic %1', TempBlob.ToBase64String()));
RequestHeaders.Add('SOAPAction', 'GetReservation:GetReservation');
RequestHeaders.Add('UseDefaultCredentials', 'TRUE');
RequestHeaders.Add('Keep-alive', 'TRUE');
Content.GetHeaders(ContentHeaders);
ContentHeaders.Remove('Content-Type');
ContentHeaders.Add('Content-Type', 'application/xml; charset=utf-8');
WebRequest.Content(Content);
WebClient.send(WebRequest, WebResponse);
The same was working in NAV development with dotnet types. but cannot able to make it work in AL. Please guide me, Thanks.
Categories:
  • Suggested answer
    Narendra Reddy Profile Picture
    Narendra Reddy 10 on at
    RE: BC Webservice calling from AL

    Thanks. I am using run time 2.3. This is only available from 3.0. Hope this is the issue with my scenario.

  • Verified answer
    I Gusti Made Ari Profile Picture
    I Gusti Made Ari 3,594 on at
    RE: BC Webservice calling from AL

    using AL Httpclient  , they mentioned in developer preview 2018  it is supporting ntlm authentication and will be shipped in next release. I haven't checked yet whether it is now inside spring 2019 release.

    community.dynamics.com/.../developer-preview-december-2018

    Using .net httpweb request you might need to try UseDefaultCredential method

  • Narendra Reddy Profile Picture
    Narendra Reddy 10 on at
    RE: BC Webservice calling from AL

    Service is setup for NTLM Authentication. How can I add method in request header?

  • Suggested answer
    I Gusti Made Ari Profile Picture
    I Gusti Made Ari 3,594 on at
    RE: BC Webservice calling from AL

    For Windows Credential you need to use NTLM Authentication method in your request header and also you need to make sure "ServicesUseNTLMAuthentication" has been enabled on your BC On-Premise service.

  • Suggested answer
    Narendra Reddy Profile Picture
    Narendra Reddy 10 on at
    RE: BC Webservice calling from AL

    Thanks for your responses.

    It is not working in my PC, which is not in any domain and the Credential Type is windows.

    But it is working with Docker/azure installation (where the Credential Type is NavUserPassword).

    My working code is:

           XmlDoc.WriteTo(RequestText);

           Content.WriteFrom(RequestText);

           Content.GetHeaders(ContentHeaders);

           ContentHeaders.Remove('Content-Type');

           ContentHeaders.Add('Content-Type', 'application/xml; charset=utf-8');

           WebRequest.Content := Content;

           WebRequest.GetHeaders(RequestHeaders);

           WebRequest.SetRequestUri(URL);

           WebRequest.Method('POST');

           RequestText := strsubstNo('%1:%2', UserIDTxt, PasswordTxt);

           TempBlob.WriteAsText(RequestText, TextEncoding::Windows);

           RequestHeaders.Add('Authorization', StrSubstNo('Basic %1', TempBlob.ToBase64String()));

           RequestHeaders.Add('Connection', 'keep-alive');

           RequestHeaders.Add('SOAPAction', MySoapAction);

           WebClient.Timeout := 30000;

           WebClient.send(WebRequest, WebResponse);

  • MohitMalik8085 Profile Picture
    MohitMalik8085 100 on at
    RE: BC Webservice calling from AL

    Hi Reddy,

    I think you can try with changing the WebRequest.SetRequestUri ... remove the localhost and use the IP address of the server as (192.168.0.0), it might be work incase of web service.

  • Narendra Reddy Profile Picture
    Narendra Reddy 10 on at
    RE: BC Webservice calling from AL

    Thanks. But didn't work out.

  • Stefano Demiliani Profile Picture
    Stefano Demiliani 37,162 Most Valuable Professional on at
    RE: BC Webservice calling from AL

    In D365BC, go to Users, select your user and in the Web Service Access section click on Web Service Access Key in order to create the key.

    Then, in your AL code use your username (without domain) and the web service access key as password.

  • Narendra Reddy Profile Picture
    Narendra Reddy 10 on at
    RE: BC Webservice calling from AL

    Hei, we are using correct credentials. If we develop in C/AL with dotnet variables it is working. But we wanted to have the solution in extension.

    I don't know how to use web service access key to the above. Please suggest, so we can try it as well.

  • Stefano Demiliani Profile Picture
    Stefano Demiliani 37,162 Most Valuable Professional on at
    RE: BC Webservice calling from AL

    Are you using the correct password? You need to use the generated web service access key.

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

November Spotlight Star - Khushbu Rajvi

Congratulations to a top community star!

Forum Structure Changes Coming on 11/8!

In our never-ending quest to help the Dynamics 365 Community members get answers faster …

Dynamics 365 Community Platform update – Oct 28

Welcome to the next edition of the Community Platform Update. This is a status …

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,269 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans