Pular para o conteúdo principal

OnSend

OnSend roda logo antes do payload sair para o cliente. Vale para todas as rotas da thread — ponto único para headers globais, auditoria ou normalização de Content-Type.

Disponível a partir do tlppCore 01.03.04.

Configure no slave (ou pool sem slaves).

Quando usar

  • Forçar Content-Type conforme o body
  • Log centralizado de respostas
  • Headers de segurança em todas as APIs

Configuração

[SLAVES_UEX]
OnSend=U_OnSend

Assinatura

Function U_OnSend()

Sem parâmetros — leia a resposta montada com oRest:getBodyResponse() e altere headers com oRest:updateKeyHeaderResponse().

Retorno ignorado.

Exemplo

Detecta se o body é JSON válido e ajusta Content-Type:

#include "tlpp-core.th"
#include "tlpp-rest.th"

Function U_OnSend()
Local cMsgSend := oRest:getBodyResponse()
Local jValue := JsonObject():New()
Local uRet := Nil

If (!Empty(cMsgSend))
uRet := jValue:FromJson(cMsgSend)

If (ValType(uRet) == "U")
oRest:updateKeyHeaderResponse("Content-Type", "application/json")
Else
oRest:updateKeyHeaderResponse("Content-Type", "text/plain")
EndIf

FreeObj(jValue)
EndIf

Return Nil

Próximo passo: OnError