Jump to content
admar

DataSnap REST Application

Recommended Posts

I am working on Delphi 12.2 datasnap REST Client Application. 
Whatever I did about it, I was not successful. I don't get any errors while working on rmGET.
However, I have not been successful in any of my efforts using rmPOST. 
No matter how I do it, I get the error
 [""error": "TServerMethods1.updateInsertFirma14 method not found in the server method list".] 
The Clint and Server side of my application are as follows. 
I use Unidac and PostgrSQL during these operations.
//CLIENT SIDE
procedure TForm1.Button8Click(Sender: TObject);

var
  LJSON: TJSONObject;
  RESTClient: TRESTClient;
  RESTRequest: TRESTRequest;
  RESTResponse: TRESTResponse;
begin
  // Kullanıcı girişlerini kontrol et
  if Edit1.Text.Trim = '' then
  begin
    ShowMessage('Lütfen bir isim girin.');
    Exit;
  end;

  if Edit2.Text.Trim = '' then
  begin
    ShowMessage('Lütfen bir soyisim girin.');
    Exit;
  end;

  // JSON nesnesini oluştur
  LJSON := TJSONObject.Create;
  try
    LJSON.AddPair('firstname', Edit1.Text.Trim);
    LJSON.AddPair('lastname', Edit2.Text.Trim);

    // REST bileşenlerini oluştur
    RESTClient := TRESTClient.Create('http://192.168.1.35:8080/datasnap/rest');
    RESTRequest := TRESTRequest.Create(nil);
    RESTResponse := TRESTResponse.Create(nil);

    try
      RESTRequest.Client := RESTClient;
      RESTRequest.Response := RESTResponse;
      RESTRequest.Resource := 'TServerMethods1/InsertFirma14';
      RESTRequest.Method := TRESTRequestMethod.rmPOST;

      // İstek gövdesini ayarla
      RESTRequest.AddBody(LJSON.ToString, ContentTypeFromString('application/json'));

      // İsteği gönder
      RESTRequest.Execute;

      // Yanıt kontrolü
      if RESTResponse.StatusCode = 200 then
      begin
        ShowMessage('Kayıt başarıyla eklendi.');
        Memo2.Lines.Add('Yanıt: ' + RESTResponse.Content);
      end
      else
      begin
        ShowMessage('Hata: ' + RESTResponse.StatusText);
      end;
    except
      on E: Exception do
        ShowMessage('Hata oluştu: ' + E.Message);
    end;

  finally
    LJSON.Free;
    RESTClient.Free;
    RESTRequest.Free;
    RESTResponse.Free;
  end;
end;

//SERVER SIDE

function TServerMethods1.InsertFirma14(const AJSON: string): String;
var
  LJSON: TJSONObject;
  JSONValue: TJSONValue;
  UniQuery1: TUniQuery;
  ResultJSON: TJSONObject;
  Firstname, Lastname: string;
begin
  ResultJSON := TJSONObject.Create;  // Yanıt olarak dönecek JSON nesnesi
  LJSON := nil;
  UniQuery1 := nil;

  try
    // Gelen JSON verisini çözümle
    try
      // JSON string'ini parse et
      JSONValue := TJSONObject.ParseJSONValue(AJSON); // Burada AJSON'ı doğrudan kullanıyoruz
      if JSONValue is TJSONObject then
        LJSON := JSONValue as TJSONObject  // JSONValue'yi TJSONObject'e dönüştür
      else
        raise Exception.Create('Geçerli bir JSON nesnesi bulunamadı.');
    except
      on E: Exception do
        raise Exception.Create('JSON parse error: ' + E.Message);
    end;

    // JSON verisi boş değilse işleme devam et
    if not Assigned(LJSON) then
      raise Exception.Create('Invalid JSON format.');

    // Veritabanı Bağlantısını Kontrol Et
    if not UniConnection1.Connected then
      raise Exception.Create('Database connection is not open.');

    // JSON'dan firstname ve lastname değerlerini al
    Firstname := LJSON.GetValue('firstname', '');  // Varsayılan olarak boş bir string dönecek
    Lastname := LJSON.GetValue('lastname', '');    // Varsayılan olarak boş bir string dönecek

    // Eğer firstname veya lastname boşsa hata oluştur
    if (Firstname = '') or (Lastname = '') then
      raise Exception.Create('Firstname or Lastname is missing in the JSON data.');

    // Query Oluşturma
    UniQuery1 := TUniQuery.Create(nil);
    try
      //UniConnection1.Commit;
      UniQuery1.Connection := UniConnection1;
      UniQuery1.SQL.Text := 'INSERT INTO Firma (firstname, lastname) VALUES (:firstname, :lastname)';
      // Parametreleri JSON nesnesinden al
      UniQuery1.Params.ParamByName('firstname').AsString := Firstname;
      UniQuery1.Params.ParamByName('lastname').AsString := Lastname;
      UniQuery1.ExecSQL;  // Veriyi veritabanına ekle

      // Başarılı işlem sonucu
      ResultJSON.AddPair('status', 'success');
      ResultJSON.AddPair('message', 'Firma kaydedildi.');
      ResultJSON.AddPair('result', TJSONNumber.Create(1));  // Başarılı sonucu 1 olarak döndür
      Result := ResultJSON.ToString;  // JSON nesnesini string olarak döndür
    finally
      FreeAndNil(UniQuery1);  // Query nesnesini serbest bırak
    end;

  except
    on E: Exception do
    begin
      // Hata durumunda yanıt dön
      ResultJSON.AddPair('status', 'error');
      ResultJSON.AddPair('message', E.Message);  // Hata mesajını döndür
      ResultJSON.AddPair('result', TJSONNumber.Create(0));  // Hata sonucu 0 olarak döndür
      Result := ResultJSON.ToString;  // JSON nesnesini string olarak döndür
    end;
  end;

  // Bellek temizliği
  FreeAndNil(LJSON);  // JSON nesnesini serbest bırak
  FreeAndNil(ResultJSON);  // Yanıt JSON nesnesini serbest bırak
end;

 

Edited by admar

Share this post


Link to post
POST
http://192.168.1.35:8080/datasnap/rest/TServerMethods1/InsertFirma14/{"firstname":"John","lastname":"TOPRAK"}  is working on explorerr

Not working ...........

curl -X POST "http://192.168.1.35:8080/datasnap/rest/TServerMethods1/InsertFirma14" \
-H "Content-Type: application/json" \
-d '{"firstname":"John","lastname":"TOPRAK"}'


POST http://192.168.1.35:8080/datasnap/rest/TServerMethods1/InsertFirma14
Content-Type: application/json

{
  "firstname": "John",
  "lastname": "TOPRAK"
}


 

Edited by admar

Share this post


Link to post

Rename the server-side function to UpdateInsertFirma14. InsertFirma14 is for GET requests.

Share this post


Link to post
18 hours ago, admar said:

is working on explorerr

Browsers mostly do GET request. How do you make a POST request using explorer?

BTW, your example code feels like you are not actually bound to DataSnap. I suggest you to check out MARS-Curiosity https://github.com/andrea-magni/MARS

Share this post


Link to post
3 hours ago, MikeMon said:

Rename the server-side function to UpdateInsertFirma14. InsertFirma14 is for GET requests.

Thanks.

 When I use rmGET, I can insert a database.
However, this type of work sends everything open via the URL.
This situation is not clear. It is readable via Postman.
It is said that it is useful to send it as a JSON object.


 

Share this post


Link to post

When I use rmGET, I can insert a database.
However, this type of work sends everything open via the URL.
This situation is not clear. It is readable via Postman.
It is said that it is useful to send it as a JSON object.

Share this post


Link to post
7 hours ago, MikeMon said:

Rename the server-side function to UpdateInsertFirma14. InsertFirma14 is for GET requests.

Thanks... 

 

 

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

×