-1

I would like to create a PostRequest in my Business Central Extension that authenticates me in my web service and returns me a token. I send my username and password in the body of the request to my web service and I also receive the token in JSON format in the body.I want to create the post request using HttpClient.

I use the following link as a template: https://learn.microsoft.com/en-us/dynamics365/business-central/dev-itpro/developer/methods-auto/httpcontent/httpcontent-data-type

    procedure sendPostRequest(uri: Text) 
        var
        client: HttpClient;
        content: HttpContent;
        contentHeaders: HttpHeaders;
        response: HttpResponseMessage;
        request: HttpRequestMessage;

    begin
        content.GetHeaders(contentHeaders);
        contentHeaders.Clear();
        contentHeaders.Add('Content-Type', 'application/json');

        request.Content:= content;

        request.SetRequestUri(uri);
        request.Method := 'POST';
    end;
    procedure SetURLsToDefault(var MessagingServiceSetup: Record "Messaging Service Setup WMR")
    begin
       MessagingServiceSetup."Service URL" := '202.212.127:8800';
    end;

And I have a couple of questions:

1) the basic url is 202.212.127:8800 for my API gateway. To be able to authenticate myself I have to access 202.212.127:8800/authenticate. Is there a method in which you can create urls?

2) how do I get my username and password in the content?

3) and how do I get the token and can I save it in the field?

can someone tell me how to get the PostRequest up and running?

Doncarlito87
  • 359
  • 1
  • 8
  • 25

2 Answers2

0

Common method to create different URLs is like this:

  • Create a setup table
  • Create fields like "Base Url", User, Pass etc.

I propose this pattern for your code:

SendRequest(Method; Url; Body)
Begin
...

Couple of functions (Your Api and Auth):

Authenticate()
begin
 Method = 'post';
 Url = SetupTable."Base Url" + '/authenticate';
 Body = (Use AL Json stack and incorporate your user pass)
 SendRequest(Method; Url; Body);
end;

Function1()
begin
 Method = 'get';
 Url = SetupTable."Base Url" + '/apiPath-Function1';
 Body = '';
 SendRequest(Method; Url; Body);
end

Function2()
begin
 Method = 'post';
 Url = SetupTable."Base Url" + '/apiPath-Function2';
 Body = (Use AL Json stack and incorporate your body structure);
 SendRequest(Method; Url; Body);
end;

To get your user pass into the content you need to check the documentation of the Api you're trying to call. It's usually described in details, it can be a simple header for basic authentication or a complex Jwt.

For receiving a token, again you need to check your Api documentation first, but essentially after making a Rest call (like: client.Send(RequestMessage, ResponseMessage); inside your SendRequest method), you get a response back and you can use AL Json stack to carve information out.

This is a fine article on how to proceed: https://jackmallender.com/2019/03/04/interacting-with-rest-apis-using-json-from-within-business-central-part-1-an-introduction-to-the-httpclient-data-type/

Babak
  • 46
  • 5
0
  1. Basically a string could work as an url. Depends on what you want. It is good practice to have a setup for your web service calls, so I am with Babak. You can set up a table in which you store the links, credentials - whatsoever.

    1. and 4) I suggest Waldos Rest App for web service calls. you can download the source here: https://github.com/waldo1001/waldo.restapp

It encapsulated the calls, has helper functions for json handling as well. Using the "REST Helper" Codeunit. You can break down your call to:

local procedure DoCallWebservice(URI: Text; User: Text; Pass: Text; var Token: Text);
var
    RESTHelper: Codeunit "REST Helper WLD";
begin
    RRESTHelper.Initialize('GET', URI);
    RESTHelper.SetContentType('application/json');
    RESTHelper.AddBody('{"user":"USERNAME","pass":"PASSWORD"}');
    if RESTHelper.Send() then
      Token := RESTHelper.GetResponseContentAsText();
end;

Obviously, you need to parse the response (JSONHelper) to your needs. Look at the code of the codeunit, it's more or less self explanatory.

Robert Kirsten
  • 474
  • 2
  • 5
  • 12