Is it possible to change the Response Code when user authorization fails in a datasnap server?

[亡魂溺海] 提交于 2019-12-09 13:17:50

问题


Is it possible to change the HTTP Response Code in my DataSnap (Delphi XE3) server when user authorization fails? Currently it's returning HTTP/1.1 500 Internal Server Error, which can happen in many other scenarios, and doesn't help describe the problem.

Along the same idea, is it possible to change the default Response Body? Currently, it's returning {"error":"USER is not authorized to perform the requested action."}, which is fine, but I'd like to return a custom JSON object if possible.

Thanks for your time!


回答1:


I ended up executing code that looked like this in TDSAuthenticationManager.OnUserAuthorize


  if valid = false then
  begin
    GetInvocationMetadata.ResponseCode := 403;
    GetInvocationMetadata.ResponseMessage := JSONResponseObject.ToString;
    GetInvocationMetadata.ResponseContent := JSONResponseOjbect.ToString;
  end



回答2:


It's possible to get the THTTPDSResponse inside a serverclass? Ex:

TServerClass1.EchoString(val: string);
begin
  GetDSResponse.Header.Add('someheader');
end;


来源:https://stackoverflow.com/questions/16674634/is-it-possible-to-change-the-response-code-when-user-authorization-fails-in-a-da

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!