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;