【问题标题】:How to change the Content-Type response in the REST DataSnap如何更改 REST DataSnap 中的 Content-Type 响应
【发布时间】:2017-01-31 12:59:51
【问题描述】:

我有一个由WebBroker REST 制作的项目,作为服务运行,我需要将Cotent-Type 响应更改为application/json,默认情况下WebBroker REST 给我带来了

内容类型'text/html; charset=ISO-8859-1'

我通过访问方法来改变响应

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

属于Data.DBXPlatform 类,但它仍然无法解决它,它会在当前类的下方添加另一个Content-Type

这只发生在WebBroker REST,如果我通过DataSnap Server创建一个项目,我通常会得到它。但我需要WebBroker REST 才能访问客户发送给我的信息。

如何使问题发生的示例。

  • DataSnap REST Application 创建一个项目

在 ServerMethods 类中,做同样的模型。

uses System.StrUtils, Data.DBXPlatform;

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

示例响应标头。

连接:关闭
内容类型:文本/html;字符集=ISO-8859-1
内容长度:25
日期:格林威治标准时间 2013 年 9 月 10 日星期二 16:41:37
编译指示:dssession=542354.126073.592372,dssessionexpires=1200000
内容类型:application/json

【问题讨论】:

    标签: json rest delphi indy datasnap


    【解决方案1】:

    在新 DataSnap WebBroker 应用程序向导中,您可以选择创建单独的服务器模块。在生成的单元WebModuleUnit1.pas中有一个变量WebModuleClass,它是TWebModule的后代。此 WebModuleClass 对象具有 TWebResponse 类型的公共属性 Response,它使您能够设置 ContentType。

    http://docwiki.embarcadero.com/Libraries/Berlin/en/Web.HTTPApp.TWebResponse.ContentType

    【讨论】:

    • 我使用 Delphi XE7,在 WebModuleUnit1.pas 中创建函数。过程 TWebModule1.WebModuleAfterDispatch(Sender: TObject; Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);开始 Response.ContentType := 'application/json';结尾;和响应头是。连接:关闭内容类型:应用程序/json; charset=ISO-8859-1 Content-Length: 25 Date: Tue, 10 Sep 2013 16:41:37 GMT Pragma: dssession=542354.126073.592372,dssessionexpires=1200000 但我只需要内容类型的应用程序/json,有?
    • 你有什么想法吗?
    猜你喜欢
    • 2021-10-28
    • 2019-07-20
    • 2013-08-13
    • 2011-11-01
    • 2012-07-28
    • 2011-02-03
    • 2013-05-02
    • 1970-01-01
    • 2013-07-10
    相关资源
    最近更新 更多