How to change the Content-Type response in the REST DataSnap

-1

I have a project made from WebBroker REST , running as a service, I need to change the response Cotent-Type to 'application/json' and by default WebBroker REST brings me as a result

  

Content-Type 'text / html; charset = ISO-8859-1 '

I change the response by accessing the

  

GetInvocationMetadata (True) .ResponseContentType = 'application / json'

of class Data.DBXPlatform , but still does not solve it, it adds another Content-Type just below the current one.

This only happens only in WebBroker REST , if I create a project with DataSnap Server I get it normally. But I need WebBroker REST to access the information the client sends me.

Example how to get the problem to occur.

  • Create a Project from the DataSnap REST Application

In the ServerMethods class, do the same template.

uses System.StrUtils, Data.DBXPlatform;

function TServerMethods1.EchoString(Value: string): string;
begin
  Result := Value;
  GetInvocationMetadata.ResponseContentType := 'application/json';
end;

At this point you will see the following response from the Header.

Connection: close
Content-Type: text / html; charset = ISO-8859-1
Content-Length: 25 Date: Tue, 10 Sep 2013 16:41:37 GMT
Pragma: dssession = 542354.126073.592372, dssessionexpires = 1200000
Content-Type: application / json

    
asked by anonymous 31.01.2017 / 13:54

1 answer

0

I was able to solve as follows, who uses WebBroker REST for a WebService from the project DataSnap REST , there is a class IdCustomHTTPServer , in that class it contains a SetHeaders function, it follows the line of code just below to be included:

if ContentType <> '' then  
  FRawHeaders.Values['Content-Type'] := ContentType;

What it does in this line of code is to get the ContentType you set and only it, Encoding charset=ISO-8859-1 it leaves it invisible, does everything right, up to Content-Lenght it calculates right to show the size of the content to be transferred.

For Content-Type to assume what you have determined, in the WebModuleUnit1 class in the AfterDispath event of TWebModule , include the line of code as the example below.

if ResponseContentType <> '' then
  Response.ContentType := ResponseContentType;

Since ResponseContentType is a global variable coming from class ServerMethods , so I can control what should be fed into Content-Type .

    
02.02.2017 / 11:37