Jump to content
Sign in to follow this  
Skrim

Coding using AI

Recommended Posts

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 :)

 

 

 

 

  • Like 1

Share this post


Link to post

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

  1. API hallucination – mixes old FireMonkey REST.* units with the newer System.Net.* ones.

  2. Copy-pasta from outdated posts – many snippets were written for Delphi XE7–10 Seattle; since 10.4 the method signatures changed.

  3. 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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×