Response codes

Hello Wagner;

In the POST method, how could we customize the response code / values? For instant when you insert the value to database, while sending the 200OK back - you want to send a SID back to client side.

In addition; what is the best way to customize the response codes on unexpected exception raised during server request?

Thank you.

If you want to send the SID back to client, you can just return it in the function result - it will be returned in the JSON response body.

For error codes, best option is to raise exceptions:

uses {...}, XData.Sys.Exceptions;
raise EXDataHttpException.Create(401, 'Some error with 401 code');

Thanks, I will look at your note for exception.

For the SID; yes, we could do that in Result value for the functions but how about the post / put methods? Where I could add SID into HTTP response?

[HttpPost, Route('')]
procedure CreateCustomer(Customer: TCustomer);

Technically in the response I like to just add a SID value and not entire body.

can't you just return a function value?

function CreateCustomer(Customer: TCustomer): Integer;

Aha! yes - of course. Thanks for tip.

In this line; how we could create a same method with different parameters list (so it could be invoked with optional parameters in client side)? Could we somehow use overload?

[HttpGet] function GetCustomerByName(Name: string): TCustomer; overload
[HttpGet] function GetCustomerByName(Name: string; Address:string): TCustomer; overload

Overload is not supported, but you can use default parameters:

Thanks Wanger; Is that declaration should use in both interface and implementation of method?

I've define one of the functions like the sample and getting the compiler error:

[dcc32 Error] CustomerServiceImpl.pas(35): E2065 Unsatisfied forward or external declaration:

The [XDefault] default attribute only needs to be in interface declaration. Other than this you need to implement your function as usual, of course the signature in implementation must be the same as in interface and you need to implement the function.

Hello Wagner; for some reason this is not working for me. May you please incorporated this to the "TCustomer" class example you sent before (the goal is to invoke this via http client)?

Assume I want to have a method to invoke the list of customers once by "Id", then by "Id and Name" and finally by "Id, Name and Country".

As you know better, in normal situation we will declare and create 3 overload functions... how could we do that by using "XDefault"?
Thank you.

It would be something like this:

function GetCustomer(Id: Integer; 
  [XDefault('')] Name: string = ''; 
  [XDefault(0)] CountryId: Integer = 0): TCustomer; 

A post was split to a new topic: Invoking XData server with Basic Auth

This topic was automatically closed 60 minutes after the last reply. New replies are no longer allowed.