Recent

Author Topic: jHttp event handlers - how do they work?  (Read 574 times)

bongorongo

  • New Member
  • *
  • Posts: 14
jHttp event handlers - how do they work?
« on: August 04, 2024, 10:23:41 pm »
I'm having big trouble using the jHttp component. I try to authenticate to a webpage and get status code 200.
I would expect a token in return, but that part of the code is never triggered.

I've tried with chatGPT alot, but it does'nt help.

Can someone please help with this?

GPT wants to put in a @ like this:
Code: Pascal  [Select][+][-]
  1.     HttpClient1.OnContentResult := @HttpClient1RequestDone;
  2.     HttpClient1.OnCodeResult := @HttpClient1CodeResult;
  3.  
But then I get error unit1.pas(43,36) Error: Variable identifier expected

Code: Pascal  [Select][+][-]
  1. unit unit1;
  2.  
  3. {$mode delphi}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, AndroidWidget, Laz_And_Controls, fpjson, jsonparser;
  9.  
  10. type
  11.  
  12.   { TAndroidModule1 }
  13.  
  14.   TAndroidModule1 = class(jForm)
  15.     btnConnect: jButton;
  16.     HttpClient1: jHttpClient;
  17.     TextView1: jTextView;
  18.     procedure AndroidModule1Create(Sender: TObject);
  19.     procedure btnConnectClick(Sender: TObject);
  20.     procedure HttpClient1RequestDone(Sender: TObject; content: RawByteString);
  21.     procedure HttpClient1CodeResult(Sender: TObject; code: Integer);
  22.   private
  23.     procedure PostData(const URL: string);
  24.   public
  25.     {public declarations}
  26.   end;
  27.  
  28. {$I settings.inc }
  29.  
  30. var
  31.   AndroidModule1: TAndroidModule1;
  32.  
  33. implementation
  34.  
  35. {$R *.lfm}
  36.  
  37. { TAndroidModule1 }
  38.  
  39. procedure TAndroidModule1.AndroidModule1Create(Sender: TObject);
  40. begin
  41.   if Assigned(HttpClient1) then
  42.   begin
  43.     HttpClient1.OnContentResult := HttpClient1RequestDone;
  44.     HttpClient1.OnCodeResult := HttpClient1CodeResult;
  45.     ShowMessage('HttpClient1 event handlers registered.');
  46.   end
  47.   else
  48.   begin
  49.     ShowMessage('HttpClient1 is not assigned.');
  50.   end;
  51. end;
  52.  
  53. procedure TAndroidModule1.btnConnectClick(Sender: TObject);
  54. begin
  55.   PostData(BaseUrl + 'oauth/token');
  56. end;
  57.  
  58.  
  59. procedure TAndroidModule1.HttpClient1CodeResult(Sender: TObject; code: Integer);
  60. begin
  61.   if code <> 200 then
  62.     TextView1.Text := 'HTTP Error: ' + IntToStr(code);
  63. end;
  64.  
  65.  
  66. procedure TAndroidModule1.HttpClient1RequestDone(Sender: TObject; content: RawByteString);
  67. var
  68.   JsonData: TJSONData;
  69.   AccessToken: string;
  70. begin
  71.   ShowMessage('Request done handler triggered');
  72.   try
  73.     JsonData := GetJSON(content);
  74.     try
  75.       AccessToken := JsonData.FindPath('access_token').AsString;
  76.       if AccessToken = '' then
  77.         AccessToken := JsonData.FindPath('data/access_token').AsString;
  78.  
  79.       if AccessToken <> '' then
  80.         ShowMessage('Token received: ' + AccessToken)
  81.       else
  82.         ShowMessage('Token not found in response.');
  83.  
  84.       TextView1.Text := 'Access Token : '+AccessToken;
  85.     finally
  86.       JsonData.Free;
  87.     end;
  88.   except
  89.     on E: Exception do
  90.     begin
  91.       TextView1.Text := 'Error parsing response: ' + E.Message;
  92.     end;
  93.   end;
  94. end;
  95.  
  96. procedure TAndroidModule1.PostData(const URL: string);
  97. begin
  98.   HttpClient1.ClearNameValueData;
  99.   HttpClient1.AddNameValueData('grant_type', 'password');
  100.   HttpClient1.AddNameValueData('username', UserName);
  101.   HttpClient1.AddNameValueData('password', Password);
  102.   HttpClient1.AddNameValueData('client_id', 'farm');
  103.   HttpClient1.AddNameValueData('scope', 'farm_manager');
  104.  
  105.   HttpClient1.AddClientHeader('Content-Type', 'application/x-www-form-urlencoded');
  106.   HttpClient1.AddClientHeader('Accept', 'application/json');
  107.  
  108.   try
  109.     HttpClient1.PostNameValueDataAsync(URL);
  110.     ShowMessage('POST request sent.' + inttostr(HttpClient1.GetResponseCode())) ;
  111.   except
  112.     on E: Exception do
  113.     begin
  114.       TextView1.Text := 'Error: ' + E.Message;
  115.     end;
  116.   end;
  117. end;
  118.  
  119. end.
  120.  
  121.  

 

TinyPortal © 2005-2018