简体   繁体   中英

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.

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM