How to change the Content-Type Response in Datasnap REST

Asked

Viewed 1,537 times

1

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

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

I change the answer by accessing the method

Getinvocationmetadata(True). Responsecontenttype = 'application/json'

class Data.DBXPlatform, but it still doesn’t solve it adds one more Content-Type just below the current.

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

Example of how to make the problem occur.

  • Create a project from the Datasnap REST Application

In the Servermethods class, do in the same template.

uses System.StrUtils, Data.DBXPlatform;

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

With this we tense the following Header response.

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

1 answer

1


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

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

What he does in that line of code is take the ContentType that you determined and only him, the Encoding charset=ISO-8859-1 he makes invisible, does everything right, until the Content-Lenght it calculates right to show the size of the content to be downloaded.

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

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

Whereas ResponseContentType is a global variable from the class ServerMethods, so I can control what should be fed on Content-Type.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.