0

Relation between REST CRUD operation and HTTP verbs are described at:

Using HTTP Methods for RESTful Services

Also, at Embarcadero REST overview it is covered with exmples:

REST overview

However, in implementation of mapping PUT and POST there is inconsistency:

Datasnap REST

Implemented behavior treat PUT as method for inserting new resource, and return code: 201(Create), but POST, as "update" prefix for related method suggest: return 200(OK).

If we put aside inconsistency in prefix of methods for PUT and POST and force regular return response code for insert new resource:

  //  User inserted in DB, obtain UserID
  LUserID := LspUserInsert.FieldByName('UserID').AsString.Substring(1,36);

  // Response on a new User inserted
  with GetInvocationMetadata(True) do
  begin
    ResponseCode := 201;
    ResponseContentType := 'application/json; charset=utf-8';
  end;
  //TODO: Set Location in the response header
  GetDataSnapWebModule.Response.Location :=  LHTTPReq + '/' + LUserID;
end;

Is there better way to implement regular mapping of PUT and POST? Also, how to get additional information for Location in the header of the response for 201(Create):

http://[host]:[port]/[request_path]...

so, the insert method can create in the response header (for example):

Location=http://localhost:8080/datasnap/rest/TTestServerMethods/Users/D813258D-F3D3-42D3-8C5B-9D392442C8D0


Edited:

Sorry for incomplete information and using not so clear terms "better way" and "additional data".

I would like to solve the following issues in a more elegant way, if there is any:

  1. Change method name. (Status: partially solved) Procedure for insert an user is named "updateUsers" due to datasnap REST prefix for POST which I want to use for create resource. The only was is to choose different name of method, but then REST request has to put method name in quotas ("insertUsers").

  2. Generate expected response on datasnap REST request. (Status: SOLVED) Default behaviour of PUT on ok response is always 201(Create), and POST responds with 200(OK). Attached part of code solved 201(create) response.

So problem is partially solved, with additional code, consent to weird methods names or request that clients create requests with quoted resources. I believe that there is a "better way" to solve present datasnap REST implementation of PUT and POST, by means of override some methods of classes doing implementation of datasnap REST. I hope Embarcadero will fix this REST "misinterpretation" in following releases.

About Location. Usually, POST creates a resource and responds with 201(create) with accompanied location of created resource in the response header. In mentioned method I get created userID from a stored procedure, however I need data coming from HTTP request URI( host, port, path_to_resource), to create complete Location string. For above example, this "additional data" is: "http://localhost:8080/datasnap/rest/TTestServerMethods/Users/"

Dejan Mitic
  • 1
  • 1
  • 3
  • Define "better way" and "additional information". "Better" is a matter of opinion, which is off-topic here on SO and "additional" depends on what information you already have. – MartynA Feb 26 '18 at 16:55

0 Answers0