#1 2022-10-09 22:37:21

talexone
Member
Registered: 2013-08-21
Posts: 23

Blob from interface based method in Mormot2

Got exception when returning blob:

procedure TUserController.GetPhoto(const Ctxt: TSQLRestServerURIContext; const ID: TID);
var
  blob: TSqlRawBlob;
begin
  Server.RetrieveBlob(TORMUser, ID, 'Photo', blob);
  Ctxt.ReturnBlob(blob, HTTP_PARTIALCONTENT, true, 'photo' + IntToStr(id));
end;

It seems Ctxt.Call is nil:

procedure TRestUriContext.ReturnBlob(const Blob: RawByteString;
  Status: integer; Handle304NotModified: boolean; const FileName: TFileName;
  CacheControlMaxAge: integer);
begin
  if not ExistsIniName(pointer(fCall^.OutHead), HEADER_CONTENT_TYPE_UPPER) then    <--- Exception here

Bug or feature?

Tried before with an out parameter, but response's Content-Type was 'application/json' instead of 'image/jpeg' and was refused by client:

procedure TUserController.GetPhoto(const ID: TID; out response: TSQLRawBlob);
begin
  Server.RetrieveBlob(TORMUser, ID, 'Photo', response);
end;

How to define a Content-Type for interface based method?

Offline

#2 2022-10-10 06:56:54

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,661
Website

Re: Blob from interface based method in Mormot2

What is this TUserController.GetPhoto signature?

If this is from an interface-based service, then Ctxt is the value sent by the client, not the current server-side execution context.

To return BLOB from an interface, use TServiceCustomAnswer kind of result.
See https://synopse.info/files/html/Synopse … #TITLE_478

Offline

#3 2022-10-10 21:04:47

talexone
Member
Registered: 2013-08-21
Posts: 23

Re: Blob from interface based method in Mormot2

Thank you for your help @ab, it's exactly what I need.

IUserController =  interface(IInvokable)
  function GetPhoto(const ID: TID): TServiceCustomAnswer;
...

TUserController = class(TInjectableObjectRest, IUserController)
  public
    function GetPhoto(const ID: TID): TServiceCustomAnswer;
...

function TUserController.GetPhoto(const ID: TID): TServiceCustomAnswer;
var
  LPhoto: TSQLRawBlob;
begin
  if Server.RetrieveBlob(TORMUser, ID, 'Photo', LPhoto) then
  begin
    result.Header := HEADER_CONTENT_TYPE+JPEG_CONTENT_TYPE;
    result.Content := LPhoto;
    result.Status := 200;
  end
  else
  begin
    result.Header := HTML_CONTENT_TYPE_HEADER;
    result.Content := 'Not found';
    result.Status := 404;
  end;
end;

Offline

Board footer

Powered by FluxBB