WebHttpResquest in execution time

What is the best approach to obtain the response from an API call and display it correctly in ShowMessage?

In the given scenario, we have two buttons that perform the same task in different ways. One button calls the function "GetApi_Runtime," which creates the request object, executes the API call, and returns the JSON in a string variable. The other button uses a pre-configured component to make the API call.

The question is:

When the component's event is triggered, the API response is correctly displayed in ShowMessage. However, when executing the "GetApi_Runtime" function, the response is not displayed in ShowMessage.

What would be the most appropriate approach to ensure that the API call response is correctly displayed in ShowMessage, regardless of whether the component or the "GetApi_Runtime" function is used?
Unit1.dfm (1.3 KB)
Unit1.pas (2.0 KB)
Unit1.html (170 Bytes)

Easiest way would be to use async.
This is explained on page 443/444 at
http://www.tmssoftware.biz/Download/Manuals/TMSWEBCoreDevGuide.pdf

I apologize for my ignorance, but I really don't know...
What is the best way to obtain the information from TJSPromises? In the example below, I am passing a function with a string return, but when making the call, it doesn't return a string, but rather a TJSPromises.

The error is on line 71 and I do not see your definition of GetApi_Runtime ?

Sorry...


You cannot do it this way. If you use the async attribute, your method is expected to return a TJSPromise.

You would as such write it like:

type
  TForm1 = class(TWebForm)
    WebHttpRequest1: TWebHttpRequest;
    WebButton1: TWebButton;
    [async]
    procedure WebButton1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    [async]
    function GetResultAsString: TJSPromise;
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

function TForm1.GetResultAsString: TJSPromise;
begin
  Result := TJSPromise.new(
    procedure(ASuccess, AFailed: TJSPromiseResolver)
    begin
      webhttprequest1.URL := 'https://download.tmssoftware.com/tmsweb/demos/TMSWEB_ResponsiveGrid/carsfull.json';
      webhttprequest1.Execute(
        procedure(AResponse: string; ARequest: TJSXMLHttpRequest)
        begin
          ASuccess(AResponse);
        end);
    end);
end;

procedure TForm1.WebButton1Click(Sender: TObject);
var
  s:string;
begin
  s := await(string, GetResultAsString);
  console.log(s);
end;

Worked perfectly!
Thank you so much...

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.