【问题标题】:Delphi - Passing Header values to REST ServicesDelphi - 将标头值传递给 REST 服务
【发布时间】:2018-02-26 17:52:02
【问题描述】:

我有一个 REST 服务。默认情况下,返回的 JSON 数据已消除所有 NULL。这给我带来了困难,所以我有一个改变行为的请求标头设置。我需要添加的请求标头是: 接受格式:json-nulls=include

我已经能够使用以下格式从 POSTMAN 开始工作。

我无法在我的应用程序中使用它。
我的应用程序有一个 TRESTClient、TrestResponse 和 TRESTRequest。

我已尝试将此作为参数添加到 TRESTClient 和 TRESTRequest。虽然 REST 服务返回数据,但未显示 NULL 字段,这告诉我我的格式(或与请求标头相关的其他内容)不正确。应该在哪里以及如何添加?
任何想法表示赞赏。

【问题讨论】:

  • 对我来说看起来不错。您是否验证了 Accept-Formatting 标头是否实际被发送?就个人而言,我不喜欢 Embarcadero 的 REST 框架。它使用起来过于复杂,而且很容易出错。我建议切换到另一个 REST 客户端。或者,只需使用 Indy 的 TIdHTTP 之类的东西手动发送您自己的 REST 请求,然后您就可以完全控制它。
  • @Remy,-不,我没有。我该怎么做?
  • 要么使用像 Wireshark 这样的数据包嗅探器来嗅探网络流量,要么让您的服务器记录它实际接收到的标头
  • 我在通过 Access-Token 标头提供 GUID 时遇到了类似的问题,需要添加 poDoNotEncode 选项,然后它工作正常

标签: rest delphi


【解决方案1】:

尝试直接从工具> 休息调试器调用,如果所有工作复制/粘贴组件到您的项目并尝试手动更改一些参数。

例子:

【讨论】:

  • Authorization 标头还需要设置[poDoNotEncode] 选项,否则将不起作用。
【解决方案2】:

如果您想以编程方式发送 firebase 通知,您可以尝试我使用的方式,它对我有用。

{Send a notification to Firebase Messaging.

 @param Titulo Notification title
 @param Mensagem Notification message
 @param Page The page the user will be redirected after the notification is clicked Ex: `/home`}
function EnviarNotificacao(Titulo, Mensagem, Page: string): string;
var
  RESTClient1: TRESTClient;
  RESTRequest1: TRESTRequest;
  RESTResponse1: TRESTResponse;
  JsonBody, JsonNotification, JsonData, JsonResponse: TJSONObject;
begin
  RESTClient1 := TRESTClient.Create(nil);
  RESTRequest1 := TRESTRequest.Create(nil);
  RESTResponse1 := TRESTResponse.Create(nil);
  RESTRequest1.Client := RESTClient1;
  RESTRequest1.Response := RESTResponse1;
  RESTRequest1.Method := TRESTRequestMethod.rmPOST;
  RESTRequest1.Params.AddItem('Authorization', 'key=AAAd...', pkHTTPHEADER, [poDoNotEncode]);
  RESTClient1.BaseURL := 'https://fcm.googleapis.com/fcm/send';

  JsonBody := TJSONObject.Create;
  JsonNotification := TJSONObject.Create;
  JsonData := TJSONObject.Create;
  JsonNotification.AddPair('title', Titulo);
  JsonNotification.AddPair('body', Mensagem);
  JsonData.AddPair('click_action', 'FLUTTER_NOTIFICATION_CLICK');
  JsonData.AddPair('page', Page);

  JsonBody.AddPair('notification', JsonNotification);
  JsonBody.AddPair('data', JsonData);
  JsonBody.AddPair('to', '/topics/your_topic');

  RESTRequest1.AddBody(JsonBody);
  RESTRequest1.Execute;
  JsonResponse := RESTResponse1.JSONValue as TJSONObject;
  Result := JsonResponse.ToString;
end;

你也应该释放TJSONObject,我没有这样做,因为我很着急。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-24
    • 1970-01-01
    • 1970-01-01
    • 2016-01-21
    • 1970-01-01
    相关资源
    最近更新 更多