Delphi - Passing Header values to REST Services

时间:2018-02-26 17:52:02

标签: rest delphi

I have a REST Service. By default, the JSON data being returned has all the NULLs eliminated. This is causing me difficulties, so I have a request header setting that changes the behavior. The request header I need to add is: Accept-Formatting: json-nulls=include

I have been able to get to work from POSTMAN, using the following format.

enter image description here

I have not been able to get this to work from my application.
My application has a TRestClient, TRestResponse and TRestRequest.

I have tried adding this as a parameter on both TRestClient and TRestRequest. While the REST service returns the data, the NULL fields are not being displayed, which tells me that my format (or something else relating to the request header) is not correct. Where and HOW should this be added?
Any thoughts appreciated.

enter image description here

2 个答案:

答案 0 :(得分:0)

如果所有有效的复制/粘贴组件都已复制到您的项目,请尝试直接从“工具”>“ Rest调试器”中调用,然后尝试手动更改一些参数。

示例:

enter image description here

答案 1 :(得分:0)

如果您想以编程方式发送 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,我没有这样做,因为我很着急。