Ver Mensaje Individual
  #9  
Antiguo 18-12-2022
genyus00 genyus00 is offline
Miembro
 
Registrado: jun 2010
Posts: 29
Reputación: 0
genyus00 Va por buen camino
Cita:
Empezado por novato_erick Ver Mensaje
Logré por fin agregar los parámetros de encabezado de esta manera:

Código Delphi [-]
procedure TuConfigura.btnGet1Click(Sender: TObject);
var
  jValue: TJSONValue;
  miparametro: string;
  RESTRequest1: TRESTRequest;
  RESTClient1: TRESTClient;
  RESTResponse1 : TRESTResponse;
begin
  try
    RESTClient1 := TRESTClient.Create(nil);
    RESTRequest1 := TRESTRequest.Create(nil);
    RESTResponse1 := TRESTResponse.Create(nil);
    try
      RESTClient1.Accept := 'application/json, text/plain; text/json';
      RESTClient1.AcceptCharset := 'UTF-8, *;q=0.8';
      RESTClient1.BaseURL := eUrlApi.Text;
      RESTClient1.HandleRedirects := True;
      RESTClient1.AcceptEncoding := 'identity';
      RESTClient1.ContentType := 'application/json';
      RESTClient1.UserAgent := 'Embarcadero RESTClient/1.0';
//      RESTClient1.SynchronizedEvents := True;
      RESTClient1.FallbackCharsetEncoding := 'UTF-8';
      RESTClient1.AllowCookies := True;


      RESTRequest1.Client := RESTClient1;
//El parametro X-IFX-Token no es un estándar definido dentro de los campos de encabezados html https://en.wikipedia.org/wiki/List_o..._header_fields
      RESTRequest1.Params.AddItem('X-IFX-Token', eToken.Text,
        TRESTRequestParameterKind.pkHTTPHEADER, [poAutoCreated]);


{En este caso aquí se presentó también el dolor de cabeza ya que usando el procedimiento
 RESTReques1.AddParameter(nName, nValue, nKind) 
la respeusta era: http://app.midominio.com/api?action=customers&page=1  Bad Request 400 -0 bytes data returnetd. blablabla}

//la solución fué agregando un Cuerpo "Body" de esta manera ya obtuve la respuesta: {"class":"GET","action":"customers","customers":[],"count":"0"}
      RESTRequest1.AddParameter('Content-Type', 'application/json',
        TRESTRequestParameterKind.pkHTTPHEADER, [poDoNotEncode]);
      RESTRequest1.AddParameter('Accept', 'application/json',
        TRESTRequestParameterKind.pkHTTPHEADER, [poDoNotEncode]); 

      miparametro := '{ "class": "GET", "action": ' + '"' + eParam1.Text + '",'
        + ' "page": "' + eParam2.Text + '" }';

      RESTRequest1.AddBody(miparametro, TRESTContentType.ctAPPLICATION_JSON);
      RESTRequest1.Method := TRESTRequestMethod.rmPOST;
      RESTRequest1.Timeout := 30000;
      RESTResponse1.ContentType := 'application/json';
      RESTRequest1.Response := RESTResponse1;
      RESTRequest1.Execute;

      jValue := RESTResponse1.JSONValue;
      if (ComboBox1.ItemIndex = 1) and (jValue is TJSONObject) then
        mRespuesta2.Text := jValue.ToString
      else
        mRespuesta2.Text := RESTResponse1.Content;

    finally
      RESTClient1.Free;
      RESTRequest1.Free;
      RESTResponse1.Free;
    end;
  except
    on E: Exception do
      ShowMessage(E.ClassName + ': ' + E.Message);
  end;
end;

Doy por solucionado este dilema por mi falta de experiencia en trabajar con aplicaciones web y Delphi honestamente me emocionó daré más aporte de la aplicación que estoy implementando para traer info de otro sistema que me da acceso al API.

Agradezco a Neftali por su colaboración

Saludos y Bendiciones a todos.
Hola tengo una duda con esta solucion: A que corresponden estos parametros?

miparametro := '{ "class": "GET", "action": ' + '"' + eParam1.Text + '",' + ' "page": "' + eParam2.Text + '" }';
Responder Con Cita