Como adicionar um json ao Body do dwParams? #345
-
Como adicionar um json ao Body do dwParams para fazer um request com o DWClientEvents?
O que estou fazendo errado? |
Beta Was this translation helpful? Give feedback.
Replies: 10 comments 5 replies
-
Qual versão do componente? Delphi ou Lazarus? |
Beta Was this translation helpful? Give feedback.
-
2.0.7 não é a última, do trunk a última é 2.0.8-29, mas ok. procedure TfPrincipal.cbTableNameEnter(Sender: TObject);
var
ClientPooler: TRESTDWIdClientPooler;
Params: TRESTDWParams;
JSONParam: TJSONParam;
begin
ConfiguraComponentes;
// Cria os componentes
ClientPooler := TRESTDWIdClientPooler.Create(nil);
ClientPooler.AuthenticationOptions := FAuthType;
Params := TRESTDWParams.Create;
Params.Encoding := ClientPooler.Encoding;
// Cria os parâmetros
JSONParam := TJSONParam.Create(ClientPooler.Encoding);
JSONParam.ParamName := 'Result';
JSONParam.ObjectDirection := odOUT;
JSONParam.ObjectValue := ovString;
JSONParam.AsString := '';
Params.Add(JSONParam);
JSONParam := TJSONParam.Create(ClientPooler.Encoding);
JSONParam.ParamName := 'Error';
JSONParam.ObjectDirection := odINOUT;
JSONParam.ObjectValue := ovBoolean;
JSONParam.AsBoolean := false;
Params.Add(JSONParam);
JSONParam := TJSONParam.Create(ClientPooler.Encoding);
JSONParam.ParamName := 'Pooler';
JSONParam.ObjectDirection := odIN;
JSONParam.ObjectValue := ovString;
JSONParam.AsString := cbPoolerDB.Selected.Text;
Params.Add(JSONParam);
try // Faz a requisição
ClientPooler.SendEvent('GetTableNames', Params);
// busca o resultado da requisição
ceTableName.Items.Delimiter := '|';
ceTableName.Items.DelimitedText := Params.ItemsString['Result'].AsString;
finally
Params.Free;
ClientPooler.Free;
end;
end; Esse código acima aí tá no demo SimpleClient |
Beta Was this translation helpful? Give feedback.
-
Deu certo. Obrigado!
Testei um request no PostMan colocando os dados no rawbody Parece que ainda estou errando em algo básico. |
Beta Was this translation helpful? Give feedback.
-
Para poder obter o body da requisição, você precisa enviar de outra forma, como você tá usando ClientEvents, ele não faz distinção de header e body, joga tudo no mesmo lugar, você precisaria filtrar pelo nome do param de acordo com a minha segunda resposta com o código. Vamos considerar que você esteja fazendo algo parecido com isso aqui: JSONParam := TJSONParam.Create(ClientPooler.Encoding);
JSONParam.ParamName := 'meusdados';
JSONParam.ObjectDirection := odIN;
JSONParam.ObjectValue := ovString;
JSONParam.AsString := meuJSON.ToJSON;
Params.Add(JSONParam); Daí no servidor, você vai puxar essa informação através do |
Beta Was this translation helpful? Give feedback.
-
Bacana! Só mais uma coisa. O que tenho que configurar para descriptografar os dados do Params automaticamente quando recebido no servidor? |
Beta Was this translation helpful? Give feedback.
-
se você tiver fazendo dessa forma aqui: |
Beta Was this translation helpful? Give feedback.
-
Bom, internamente ele faz essa validação e decripta se tiver configurado pra decriptar, tenta testar isso na 2.1 e veja se o problema persiste, se persistir, recomendo abrir uma tarefa detalhando o problema. |
Beta Was this translation helpful? Give feedback.
-
Testei na versão 2.1 e o problema persiste. Muito obrigado pela ajuda! |
Beta Was this translation helpful? Give feedback.
-
Estou prestes a abrir a tarefa, mas ainda temo que eu ainda seja o problema. No ClientPooler em CriptOptions defina como:
Envie uma requisição com ClientEvents:
Em CriptOptions no ServicePooler defina da mesma forma que ClientPooler.
Irá obter o erro: Value 'USUARIO' not found. |
Beta Was this translation helpful? Give feedback.
-
Mas porque você não faz da forma que mostrei acima? Dessa forma aí parece confuso... |
Beta Was this translation helpful? Give feedback.
2.0.7 não é a última, do trunk a última é 2.0.8-29, mas ok.
Pra usar o clientEvents você precisa construir o objeto param antes de adicionar ele no DWParams, tipo assim: