Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
315 views
in Technique[技术] by (71.8m points)

api - Delphi DataSnap TDSServiceException

I'm using Delphi on RAD Studio 10 Seattle to work with a REST API with DataSnap. It's working on most of the HTTP verbs, except with DELETE. There's the function for it, according to the DataSnap documentation, but when I do a request with the DELETE HTTP Verb, it gives-me the exception "Project RESTApi.exe raised exception class TDSServiceException with message 'Command closed or unassigned".

Follow the code of the DELETE function for the DataSnap API.

function TSM.CancelLista(const ID_LISTA: integer): TJSONObject;
const
 _DELETE = 'DELETE FROM listas WHERE id = :id';
begin
 with FormPrincipal do
 begin
  DB_Query.Active := false;
  DB_Query.SQL.Text := _DELETE;
  DB_Query.ParamByName('id').Value :=  ID_LISTA;

  Try
    DB_Query.ExecSQL;
    Result.AddPair('Response', 'Lista atualizada com sucesso');
  Except on E : Exception do
    Result.AddPair('Response', E.Message);
  End;
 end;
end;

I'm making the requests with a PHP Code Igniter 4.0.4 web app, but already tried it with the Delphi REST Debugger, with the same result.

question from:https://stackoverflow.com/questions/65926037/delphi-datasnap-tdsserviceexception

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The issue wasn't in the API at all, but the requested method wasn't made properly, the method was being sent as ``DELETE`, so the DataSnap API wasn't recognizing it as a command. As the request as being made by Code Igniter 4.0.4 CURLRequest class as following:

$reqConfig = [            
        'headers' => [
            'Accept' => 'application/json',
            'Content-Type' => 'application/json'
        ]            
    ]; 
$verb = '`DELETE`'
$curl->request($verb, 'Listas/'.id_lista, $reqConfig);

It was solved changing the $verb taking the "`" out.

The API DELETE function is now:

function TSM.CancelLista(const ID_LISTA: integer): TJSONObject;
const
  _DELETE = 'DELETE FROM listas WHERE id = :id';
begin
  Result := TJSONObject.Create;
  with FormPrincipal do
  begin
    DB_Query.Active := false;
    DB_Query.SQL.Text := _DELETE;
    DB_Query.ParamByName('id').Value :=  ID_LISTA;

    Try
      DB_Query.ExecSQL;
      Result.AddPair('Response', 'Lista deletada com sucesso');
    Except on E : Exception do
      Result.AddPair('Response', E.Message);
    End;
  end;
end;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...