Lazarus

Programming => Networking and Web Programming => Topic started by: Ever on April 27, 2025, 04:57:00 am

Title: [SOLVED] Problems accessing information in JSON format from a URL
Post by: Ever on April 27, 2025, 04:57:00 am
Greetings

I need help resolving the following situation.

I need to access a web page with the following address
"https://bcv.ingeint.com/api/bcv/rate"

This page displays a JSON file with two fields: rate and date. Attached image with the search result

I have tried several codes to access this data, but none of them work. In all cases, the name of a JSON file must be explicitly indicated along with the URL, something like
"https://bcv.ingeint.com/api/bcv/rate/factor.json"

But this is not the case.

This page is used to calculate the exchange rate of the dollar against my country's currency. It is updated daily. I want to check this value every day to update it in a database field to execute financial transactions.

I appreciate any support you can provide.
Title: Re: Problems accessing information in JSON format from a URL
Post by: Remy Lebeau on April 27, 2025, 05:44:05 am
I have tried several codes to access this data, but none of them work.

What EXACTLY have you tried? Please show the actual code. If multiple codes are not working, then either there is a common element that is failing for all of them, or you are likely just using them wrong.

In all cases, the name of a JSON file must be explicitly indicated along with the URL, something like
"https://bcv.ingeint.com/api/bcv/rate/factor.json"

If you don't have to specify a filename in a browser, then you don't have to specify it in code, either. No HTTP client should require this.
Title: Re: Problems accessing information in JSON format from a URL
Post by: paweld on April 27, 2025, 06:54:23 am
Code: Pascal  [Select][+][-]
  1. uses
  2.   fphttpclient, opensslsockets, fpjson, jsonparser;
  3.  
  4. procedure TForm1.Button1Click(Sender: TObject);
  5. var
  6.   hc: TFPHttpClient;
  7.   url: String = 'https://bcv.ingeint.com/api/bcv/rate';
  8.   json: String = '';
  9.   jd: TJsonData;
  10.   jo: TJsonObject;
  11. begin
  12.   hc := TFPHttpClient.Create(nil);
  13.   try
  14.     json := hc.Get(url);
  15.   except
  16.     ShowMessage(Format('Error: [%d] - %s', [hc.ResponseStatusCode, hc.ResponseStatusText]));
  17.   end;
  18.   if json <> '' then
  19.   begin
  20.     jd := GetJson(json);
  21.     if jd <> nil then
  22.     begin
  23.       jo := TJsonObject(jd);
  24.       if (jo.Find('rate') <> nil) and (jo.Find('date') <> nil) then
  25.         ShowMessage(Format('Rate: %g from: %s', [jo.Find('rate').AsFloat, jo.Find('date').AsString]))
  26.       else
  27.         ShowMessage('Incorect json:'#13#10 + json);
  28.       jd.Free;
  29.     end;
  30.   end;
  31.   hc.Free;
  32. end;
Title: Problems accessing information in JSON format from a URL
Post by: Ever on April 27, 2025, 01:41:45 pm
Thanks paweld, it worked perfectly for me.
TinyPortal © 2005-2018