Jump to content
Sign in to follow this  
Carlos_Reyes

I need help with TRestRequest!

Recommended Posts

Dear colleagues, I don't know if you can help me, I don't know what to do anymore
I comment:
I need to send a json through the body of the component of type raw-json using the postman application it works fine but when I want to implement it in delphi it returns the 404 code.

but when I try to implement in delphi 11.3 it can't I don't know what I'm doing wrong

      TRestClient.NestedElements := False;
      TRestClient.ResetToDefaults;
      TRestClient.BaseURL         := 'https://localhost:8113/firmardocumento/';
      TRestClient.SecureProtocols := [];
      TRestClient.Accept          := '*/*';
      TRestClient.ContentType     := 'application/json';

      ////////////////////////////////////////////////////////////////////////
      TRestRequest.Accept   := '*/*';
      TRestRequest.Params.Clear;

      TRestRequest.AddParameter('contentType', 'application/JSON', TRESTRequestParameterKind.pkHTTPHEADER, [poDoNotEncode]);

      ////////////////////////////////////////////////////////////////////////
      TRestRequest.ClearBody;
      TRestRequest.AddBody(DTEJson, TRESTContentType.ctAPPLICATION_JSON);

      ////////////////////////////////////////////////////////////////////////
      TRestRequest.Method := TRESTRequestMethod.rmPOST;
      TRestRequest.Execute;

I have tried in many ways but it always returns the code 404

this is the way the json is created and sent

function TForm1.GetJson: string;
begin
  Result := '{'
              + '"mode":"raw",'
              + '"raw:"'
              + '{' + EOL
              + '    "nit": "06142101771068",' + EOL
              + '    "activo": "True",' + EOL
              + '    "passwordPri": "",' + EOL
              + '    "dteJson": {' + EOL
              + '        "identificacion": {' + EOL
              + '            "version": 3,' + EOL
              + '            "ambiente": "01",' + EOL
              + '            "tipoDte": "03",' + EOL
              + '            "numeroControl": "DTE-03-00100101-000000000000001",' + EOL
              + '            "codigoGeneracion": "44A4FE71-8611-4130-8B9B-F0B0A4E2AAF8",' + EOL
              + '            "tipoModelo": 1,' + EOL
              + '            "tipoOperacion": 1,' + EOL
              + '            "tipoContingencia": null,' + EOL
              + '            "motivoContin": null,' + EOL
              + '            "fecEmi": "2023-03-12",' + EOL
              + '            "horEmi": "10:50:48",' + EOL
              + '            "tipoMoneda": "USD"' + EOL
              + '        },' + EOL
              + '        "documentoRelacionado": null,' + EOL
              + '        "emisor": {' + EOL
              + '            "nit": "00932969-7",' + EOL
              + '            "nrc": "161310-4",' + EOL
              + '            "nombre": "RIVERA MELGAR, MARIA IDALIA",' + EOL
              + '            "codActividad": "79110",' + EOL
              + '            "descActividad": "Actividades de agencias de viajes y organizadores de viajes; actividades de asistencia a turistas",' + EOL
              + '            "nombreComercial": null,' + EOL
              + '            "tipoEstablecimiento": "02",' + EOL
              + '            "direccion": {' + EOL
              + '                "departamento": "08",' + EOL
              + '                "municipio ": "22",' + EOL
              + '                "complemento": "1 AV. SUR, FRENTE A CAJA DE CRÉDITO ZACATECOLUCA, DEPARTAMENTO DE LA PAZ"' + EOL
              + '            },' + EOL
              + '            "telefono": "",' + EOL
              + '            "correo": "",' + EOL
              + '            "codEstableMH": null,' + EOL
              + '            "codEstable": null,' + EOL
              + '            "codPuntoVentaMH": null,' + EOL
              + '            "codPuntoVenta": null' + EOL
              + '        },' + EOL
              + '        "receptor": {' + EOL
              + '            "nit": "02283005-7",' + EOL
              + '            "nrc": "138504-6",' + EOL
              + '            "nombre": "104-333 MILTON ODIR VALLADARES  ACEVEDO",' + EOL
              + '            "codActividad": "94110",' + EOL
              + '            "descActividad": "Actividades de organizaciones empresariales y de empleadores",' + EOL
              + '            "nombreComercial": null,' + EOL
              + '            "direccion": {' + EOL
              + '                "departamento": "02",' + EOL
              + '                "municipio": "03",' + EOL
              + '                "complemento": "02283005-7"' + EOL
              + '            },' + EOL
              + '            "telefono": "7774-1113",' + EOL
              + '            "correo": "carlosreyes210177@gmail.com"' + EOL
              + '        },' + EOL
              + '        "otrosDocumentos": null,' + EOL
              + '        "ventaTercero": null,' + EOL
              + '        "cuerpoDocumento": [' + EOL
              + '            {' + EOL
              + '                "numItem": 1,' + EOL
              + '                "tipoItem": 3,' + EOL
              + '                "numeroDocumento": null,' + EOL
              + '                "codigo": 3,' + EOL
              + '                "codTributo": null,' + EOL
              + '                "descripcion": "MESYGEST ANTICONCEPTIVO MENSUAL INYECCIÓN FECHA CALENDARIO",' + EOL
              + '                "cantidad": 1,' + EOL
              + '                "uniMedida": 59,' + EOL
              + '                "precioUni": 4.43362,' + EOL
              + '                "montoDescu": 0,' + EOL
              + '                "ventaNoSuj": 0,' + EOL
              + '                "ventaExenta": 0,' + EOL
              + '                "ventaGravada": 4.43362,' + EOL
              + '                "tributos": [' + EOL
              + '                    "20"' + EOL
              + '                ],' + EOL
              + '                "psv": 0,' + EOL
              + '                "noGravado": 0' + EOL
              + '            },' + EOL
              + '            {' + EOL
              + '                "numItem": 2,' + EOL
              + '                "tipoItem": 3,' + EOL
              + '                "numeroDocumento": null,' + EOL
              + '                "codigo": 6,' + EOL
              + '                "codTributo": null,' + EOL
              + '                "descripcion": "TIAMINA B1 VIJOSA VIAL 10 ML",' + EOL
              + '                "cantidad": 1,' + EOL
              + '                "uniMedida": 59,' + EOL
              + '                "precioUni": 2,' + EOL
              + '                "montoDescu": 0,' + EOL
              + '                "ventaNoSuj": 0,' + EOL
              + '                "ventaExenta": 0,' + EOL
              + '                "ventaGravada": 2,' + EOL
              + '                "tributos": [' + EOL
              + '                    "20"' + EOL
              + '                ],' + EOL
              + '                "psv": 0,' + EOL
              + '                "noGravado": 0' + EOL
              + '            },' + EOL
              + '            {' + EOL
              + '                "numItem": 3,' + EOL
              + '                "tipoItem": 3,' + EOL
              + '                "numeroDocumento": null,' + EOL
              + '                "codigo": 9,' + EOL
              + '                "codTributo": null,' + EOL
              + '                "descripcion": "PROGESTIN 100 (PROGESTERONA 100MG/ ML) SOLUCION INYECTRABLE",' + EOL
              + '                "cantidad": 1,' + EOL
              + '                "uniMedida": 59,' + EOL
              + '                "precioUni": 3.19469,' + EOL
              + '                "montoDescu": 0,' + EOL
              + '                "ventaNoSuj": 0,' + EOL
              + '                "ventaExenta": 0,' + EOL
              + '                "ventaGravada": 3.19469,' + EOL
              + '                "tributos": [' + EOL
              + '                    "20"' + EOL
              + '                ],' + EOL
              + '                "psv": 0,' + EOL
              + '                "noGravado": 0' + EOL
              + '            }' + EOL
              + '        ],' + EOL
              + '        "resumen": {' + EOL
              + '            "totalNoSuj": 0,' + EOL
              + '            "totalExenta": 0,' + EOL
              + '            "totalGravada": 9.62831,' + EOL
              + '            "subTotalVentas": 9.62831,' + EOL
              + '            "descuNoSuj": 0,' + EOL
              + '            "descuExenta": 0,' + EOL
              + '            "descuGravada": 0,' + EOL
              + '            "porcentajeDescuento": 0,' + EOL
              + '            "totalDescu": 0,' + EOL
              + '            "tributos": [' + EOL
              + '                {' + EOL
              + '                    "codigo": "20",' + EOL
              + '                    "descripcion": "Impuesto al Valor Agregado13%",' + EOL
              + '                    "valor": 13' + EOL
              + '                }' + EOL
              + '            ],' + EOL
              + '            "subTotal": 9.62831,' + EOL
              + '            "ivaPerci1": 0,' + EOL
              + '            "ivaRete1": 0,' + EOL
              + '            "reteRenta": 0,' + EOL
              + '            "montoTotalOperacion": 10.87999,' + EOL
              + '            "totalNoGravado": 0,' + EOL
              + '            "totalPagar": 10.87999,' + EOL
              + '            "totalLetras": "Diez con 88/100",' + EOL
              + '            "saldoFavor": 0,' + EOL
              + '            "condicionOperacion": 1,' + EOL
              + '            "pagos": [' + EOL
              + '                {' + EOL
              + '                    "codigo": "01",' + EOL
              + '                    "montoPago": 10.87999,' + EOL
              + '                    "plazo": null,' + EOL
              + '                    "referencia": "",' + EOL
              + '                    "periodo": null' + EOL
              + '                }' + EOL
              + '            ],' + EOL
              + '            "numPagoElectronico": null' + EOL
              + '        },' + EOL
              + '        "extension": {' + EOL
              + '            "nombEntrega": null,' + EOL
              + '            "docuEntrega": null,' + EOL
              + '            "nombRecibe": null,' + EOL
              + '            "docuRecibe": null,' + EOL
              + '            "observaciones": null,' + EOL
              + '            "placaVehiculo": null' + EOL
              + '        },' + EOL
              + '        "apendice": null' + EOL
              + '    }' + EOL
              + '}' + EOL
              + '}';
end;

 

PostMan.png

Share this post


Link to post
TRestRequest.AddParameter('contentType', 'application/JSON',

 In the Screenshot cURL snippet, it is 'Content-Type: application/json'. (contentType seems to be wrong)

Edited by mjustin

Share this post


Link to post

Hello, I think you speak spanish (if you dont, just tell me)

Estoy trabajando un cliente similar, (DTEs para la factura electrónica), 

yo uso visual foxpro y tiene comandos equivalentes, prácticamente la sintaxis la misma, pero yo recibo el mensaje 500:

{
    "timestamp""2024-02-05T20:49:38.354+0000",
    "status"500,
    "error""Internal Server Error",
    "message""No message available",
    "path""/firmardocumento/"
}
Creo que tiene que ver con los certificados a las claves

Share this post


Link to post

 Not sure if you figured it out, but the code below is working for me.

 

And  for me I see a diff from your code in

 

       RESTClient1.HandleRedirects:=true;
      RESTRequest1.Method :=TRESTRequestMethod.rmPOST;

 

 

endpoint := 'https://something/';
content_type := 'application/json';

// ++ some toher params

RESTClient1   := TRESTClient.Create(nil);
   RESTRequest1  := TRESTRequest.Create(nil);
   RESTResponse1 := TRESTResponse.Create(nil);
   try
     RESTRequest1.Client   :=  RESTClient1;
     RESTRequest1.Response :=  RESTResponse1;


      RESTClient1.BaseURL:=endpoint;
      RESTClient1.HandleRedirects:=true;
      RESTRequest1.Method :=TRESTRequestMethod.rmPOST;
      RestRequest1.Params.Clear;
      RestRequest1.Params.AddItem('content-type', content_type ,pkHTTPHEADER,[poDoNotEncode] );
      RestRequest1.Params.AddItem('x-amz-Date', amzdate ,pkHTTPHEADER  );
      RestRequest1.Params.AddItem('Authorization',authorization_header  ,pkHTTPHEADER ,[poDoNotEncode]);
      RestRequest1.Params.AddItem('x-amz-content-sha256', payload_hash ,pkHTTPHEADER ,[poDoNotEncode] );
      RestRequest1.AddBody(request_parameters , TRESTContentType.ctAPPLICATION_JSON );

        try
         RESTRequest1.Execute;
         writeln('respons');
         writeln( RESTResponse1.Content);
        except
          On E: Exception do
            writeln(  e.Message);
        end;
   finally
     RESTClient1.Free;
     RESTRequest1.Free;
     RESTResponse1.Free;
   end;

 

But I also set the body Json using system.json  unit

 

    Jrequest_parameters    := TjsonObject.Create;
    try
     Jrequest_parameters.AddPair(TJSONString.Create('fromhost'),TJSONString.Create('10.211.55.2'));
     Jrequest_parameters.AddPair(TJSONString.Create('fromport'),TJSONString.Create('3306'));
     Jrequest_parameters.AddPair(TJSONString.Create('fromschema'),TJSONString.Create('atest'));
     Jrequest_parameters.AddPair(TJSONString.Create('frompass'),TJSONString.Create('xxxxx'));
     Jrequest_parameters.AddPair(TJSONString.Create('tohost'),TJSONString.Create('10.211.55.2'));
     Jrequest_parameters.AddPair(TJSONString.Create('toport'),TJSONString.Create('3306'));
     Jrequest_parameters.AddPair(TJSONString.Create('toschema'),TJSONString.Create('atest2'));
     Jrequest_parameters.AddPair(TJSONString.Create('topass'),TJSONString.Create('xxxxx'));
     Jrequest_parameters.AddPair(TJSONString.Create('table'),TJSONString.Create('hseq_crm'));
     Jrequest_parameters.AddPair(TJSONString.Create('copylogs'),TJSONString.Create('0'));
     request_parameters := Jrequest_parameters.ToString;
    finally
      Jrequest_parameters.Free;
    end;

 

 

 

 

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×