Delphi-将头值传递给REST服务
我有休息服务。默认情况下,返回的JSON数据消除了所有空值。这给我带来了困难,所以我有一个请求头设置来更改行为。我需要添加的请求头是: 接受格式:json nulls=include 我已经能够从邮递员那里开始工作,使用以下格式 我无法从我的应用程序中实现此功能。Delphi-将头值传递给REST服务,rest,delphi,Rest,Delphi,我有休息服务。默认情况下,返回的JSON数据消除了所有空值。这给我带来了困难,所以我有一个请求头设置来更改行为。我需要添加的请求头是: 接受格式:json nulls=include 我已经能够从邮递员那里开始工作,使用以下格式 我无法从我的应用程序中实现此功能。 我的应用程序有一个TresClient、TresResponse和TresRequest 我已经尝试在TRestClient和TRestRequest上添加这个参数。REST服务返回数据时,不会显示空字段,这表明我的格式(或与请求头
我的应用程序有一个TresClient、TresResponse和TresRequest 我已经尝试在TRestClient和TRestRequest上添加这个参数。REST服务返回数据时,不会显示空字段,这表明我的格式(或与请求头相关的其他内容)不正确。应在何处以及如何添加此内容?
任何想法都值得赞赏
如果所有正在工作的组件都复制/粘贴到项目中,请尝试直接从“工具”>“Rest调试器”调用,并尝试手动更改某些参数 例如:
如果您希望以编程方式发送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
,我没有这样做是因为我很匆忙。看起来不错。是否验证是否确实发送了接受格式设置标题?就个人而言,我不喜欢Embarcadero的REST框架。它的使用过于复杂,而且相当麻烦。我建议切换到另一个REST客户机。或者,只需使用Indy'sTIdHTTP
之类的工具手动发送您自己的REST请求,您就可以完全控制它了。@Remy,-不,我没有。如何做到这一点?要么使用数据包嗅探器(如Wireshark)嗅探网络流量,要么让服务器记录其实际接收的头。我遇到了类似的问题,通过访问令牌头提供GUID,需要添加poDoNotEncode选项,然后工作正常。您还应该设置[poDoNotEncode]
用于授权标题的选项,否则,它将不起作用。