#1 2017-12-14 13:36:55

zensan
Member
Registered: 2015-07-29
Posts: 12

THttpServerRequest interface based REST server and response code

Hi!

I am using your HTTPServer for providing REST access to the API of a certain system. All is fine, but I can't figure out how can I do one thing.
So I was using the Sample 14 for the start. So I do TInterfaceFactory.RegisterInterfaces([TypeInfo(IMyInterfaceName)]) and so on (so create the TSQLModel, TSQLRestServerFullMemory and TRestHTTPServer.

When I have the method like UserDisplayInfo(ID: Integer): TUserDisplayInfo then I get the response with 200 OK and the JSON of the record which is all fine.
But when the user is not found by the ID we wish to return the response code 404 (for example) or some other code accordingly to the standard..

How that can be done?

I thought first that maybe SynXXX has some kind of raise ESynHTTPErrorCode.Create(AErrorCode: Cardinal; AMessage: String)... but when I look at handling of the Request method of the HTTP server I see that the responses are kind of hard-coded. And I can only override them if I override the Request method - which actually is not so convinient probably..

Thanks in advance!

Last edited by zensan (2017-12-14 13:38:57)

Offline

#2 2017-12-14 13:57:48

zensan
Member
Registered: 2015-07-29
Posts: 12

Re: THttpServerRequest interface based REST server and response code

Should I use OnErrorURI event of the TSQLRestServer. Is that the right approach?

Just for the record it looks like that actually does the job. I just check the exception class of the event OnErrorURI parameter and if it is of my ERESTServerError then I return false and set the error from the properties of the exception.

Last edited by zensan (2017-12-14 14:23:33)

Offline

Board footer

Powered by FluxBB