0
votes

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.

1
Are you certain the exception occurs on the line DB_Query.ExecSQL? If you put a breakpoint on that line and press F7, where exactly does the exception occur? What datatype is DB_Query?MartynA
No, I think it occurs even before it enters the function. I put the breakpoint on the very beginning of the function and it doesn't even is reached. Seems like when the request with the DELETE verb is made and received by the DataSnap API it already triggers the exceptionRichard Lucas
DB_Query is a FireDAC TFDQuery.Richard Lucas
I thought it might be something like that. IME, the best way to debug this would be to add a client module to the server (including a form to operate it) and then debug it from there.MartynA
I've tried something like calling the function on another unit, and it seems to be working properly. The issue starts when a HTTP DELETE request is addressed to the API.Richard Lucas

1 Answers

1
votes

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;