#1 2015-10-30 18:53:27

avista
Member
Registered: 2014-01-06
Posts: 63

How to Return Empy Content with TServiceCustomAnswer?

In general, I'm using TServiceCustomAnswer to return content from my interface based service.

However, there are some cases where I don't want to return any content (i.e., after submitting form data). 

If I set Content to an empty string, the default JSON response is still returned:

My Code (Interface Based)

function TMyService.UploadFile: TServiceCustomAnswer;
begin
  DoGetFileData(ServiceContext.Request);
  Result.Header := '';
  Result.Content := '';
  Result.Status := HTML_SUCCESS;
end;

Response Header (Interface Based)

HTTP/1.1 200 OK
Content-Type: application/json; charset=UTF-8
Server: mORMot/1.18.2017 (Windows) Microsoft-HTTPAPI/2.0
X-Powered-By: Synopse mORMot 1.18.2017 http://synopse.info
Server-InternalState: 14
Accept-Encoding: synlz,gzip
Date: Fri, 30 Oct 2015 18:31:23 GMT
Content-Length: 64

Response Body

{"result":[{"Header":"","Content":null,"Status":200}],"id":5692}

What I need is to be able to specify a return status with EMPTY content.  The same code I use in a method based service returns the following:

My Code (Method Based)

function TMyServer.UploadFile(Ctxt: TSQLRestServerURIContext);
begin
  DoGetFileData(Ctxt);
  Ctxt.Success;
end;

Response Header

HTTP/1.1 200 OK
Server: mORMot/1.18.2017 (Windows) Microsoft-HTTPAPI/2.0
X-Powered-By: Synopse mORMot 1.18.2017 http://synopse.info
Server-InternalState: 14
Accept-Encoding: synlz,gzip
Date: Fri, 30 Oct 2015 18:29:44 GMT
Content-Length: 0

This is the behavior I'm looking for.  Is there a way to ensure that no content is returned when setting TServiceCustomAnswer.Content to an empty string?

(And to be clear, I want to use the interface-based service because there is other supporting code in that class for many functions, including database access, that I don't want to duplicate)

:)

Thanks!

Offline

#2 2015-10-31 17:14:10

avista
Member
Registered: 2014-01-06
Posts: 63

Re: How to Return Empy Content with TServiceCustomAnswer?

Setting the Header to TEXT_CONTENT_TYPE_HEADER solved the problem smile :

function TMyService.UploadFile: TServiceCustomAnswer;
begin
  DoGetFileData(ServiceContext.Request);
  Result.Header := TEXT_CONTENT_TYPE_HEADER; <============
  Result.Content := '';
  Result.Status := HTML_SUCCESS;
end;

Offline

Board footer

Powered by FluxBB