Skrim 13 Posted Sunday at 06:09 AM I have just subscribed to an AI service. I already have working code for a REST service. Tested with code fram AI and OMG it looked impressive. My question was "How can I connect to REST service xxxx using Delphi 12.2?" AI gave me three times the amount of code I already use and the AI code does not work, even after several corrections. Non programmers making programs using AI, I think not :) 1 Share this post Link to post
Maxidonkey 13 Posted Sunday at 12:55 PM Hey, Delphi’s built-ins already cover everything the AI tried to re-code. Here’s the absolute minimum—25 lines, no more—tested on Delphi 12.1 uses System.Net.URLClient, System.Net.HttpClient, System.Net.HttpClientComponent, System.JSON; procedure CallService; var Http : TNetHTTPClient; Response : IHTTPResponse; Payload : TJSONObject; begin Http := TNetHTTPClient.Create(nil); try // auth ➜ replace XXX Http.CustomHeaders['Authorization'] := 'Bearer XXX'; // timeouts, proxy… if needed Response := Http.Get('https://api.your-service.com/v1/endpoint'); if Response.StatusCode = 200 then begin Payload := TJSONObject.ParseJSONValue(Response.ContentAsString) as TJSONObject; try // process your data here Writeln(Payload.ToJSON); finally Payload.Free; end; end else raise Exception.CreateFmt('HTTP %d – %s', [Response.StatusCode, Response.StatusText]); finally Http.Free; end; end; Auth: shown with a simple Bearer token—swap in OAuth2, HMAC, etc., as needed. POST / PUT: Http.Post(URL, TStringStream.Create(JSON, TEncoding.UTF8), nil, ['Content-Type', 'application/json']); Skip TRESTClient if you’re only making a couple of calls; it just wraps TNetHTTPClient and adds another mapping layer. Why the AI went off the rails API hallucination – mixes old FireMonkey REST.* units with the newer System.Net.* ones. Copy-pasta from outdated posts – many snippets were written for Delphi XE7–10 Seattle; since 10.4 the method signatures changed. No compile-fix loop – the model never actually builds the code, it just invents. Hope that saves you from wading through 300 generated lines. When the AI starts free-styling, fall back to Delphi’s native API—shorter and, most important, it compiles. Ping me if you get stuck on auth or JSON parsing. Share this post Link to post