Recent

Author Topic: Help to convert code in Indy  (Read 451 times)

laguna

  • Sr. Member
  • ****
  • Posts: 323
Help to convert code in Indy
« on: November 28, 2022, 06:17:51 pm »
I ask you for help in converting this XML file submission procedure to lazarus using its components.
The program must work on Linux and Mac and I cannot make use of paid DLLs since the submission program is a free product.
You can help me thanks.

Code: Pascal  [Select][+][-]
  1.  
  2. procedure TForm1.Button1Click(Sender: TObject);
  3. var
  4. http: HCkHttp;
  5. success: Boolean;
  6. sbSignedXml: HCkStringBuilder;
  7. json: HCkJsonObject;
  8. resp: HCkHttpResponse;
  9. sbResponseBody: HCkStringBuilder;
  10. jResp: HCkJsonObject;
  11. respStatusCode: Integer;
  12. errorCode: PWideChar;
  13. errorDescription: PWideChar;
  14. requestID: PWideChar;
  15.  
  16. begin
  17. // This example assumes the Chilkat API to have been previously unlocked.
  18. // See Global Unlock Sample for sample code.
  19.  
  20. http := CkHttp_Create();
  21.  
  22. // Implements the following CURL command:
  23.  
  24. // curl -X POST https://auth.fatturazioneelettronica.aruba.it/services/ClientRequest/CreateTransmissionRequest \
  25. //   -H "Accept: application/json" \
  26. //   -H "Content-Type: application/json;charset=UTF-8" \
  27. //   -d '{
  28. //   "userName" : "ARUBA0000",
  29. //   "password" : "ArubaPwd",
  30. //   "userID" : "ARUBA0000",
  31. //   "comunicationType" : "LI",
  32. //   "dataFile" : "dGVzdA=="
  33. // }'
  34.  
  35. // Use the following online tool to generate HTTP code from a CURL command
  36. // Convert a cURL Command to HTTP Source Code
  37.  
  38. // Use this online tool to generate code from sample JSON:
  39. // Generate Code to Create JSON
  40.  
  41. // The following JSON is sent in the request body.
  42.  
  43. // {
  44. //   "userName": "ARUBA0000",
  45. //   "password": "ArubaPwd",
  46. //   "userID": "ARUBA0000",
  47. //   "comunicationType": "LI",
  48. //   "dataFile": "dGVzdA=="
  49. // }
  50.  
  51. // The dataFile field must contain an xml document which conforms to the Revenue Agency specifications.
  52. // The document must be Base64 codified and contain a CADES-based electronic signature.
  53. sbSignedXml := CkStringBuilder_Create();
  54. success := CkStringBuilder_LoadFile(sbSignedXml,'IT01873080855_00310.xml','utf-8');
  55.  
  56. json := CkJsonObject_Create();
  57. CkJsonObject_UpdateString(json,'userName','ARUBA0000');
  58. CkJsonObject_UpdateString(json,'password','ArubaPwd');
  59. CkJsonObject_UpdateString(json,'userID','ARUBA0000');
  60. CkJsonObject_UpdateString(json,'comunicationType','LI');
  61. CkJsonObject_UpdateString(json,'dataFile',CkStringBuilder__getEncoded(sbSignedXml,'base64','utf-8'));
  62.  
  63. CkHttp_SetRequestHeader(http,'Content-Type','application/json;charset=UTF-8');
  64. CkHttp_SetRequestHeader(http,'Accept','application/json');
  65.  
  66. resp := CkHttp_PostJson3(http,'https://ws.fatturazioneelettronica.aruba.it/services/ClientRequest/CreateTransmissionRequest','application/json',json);
  67. if (CkHttp_getLastMethodSuccess(http) = False) then
  68.   begin
  69.     Memo1.Lines.Add(CkHttp__lastErrorText(http));
  70.     Exit;
  71.   end;
  72.  
  73. sbResponseBody := CkStringBuilder_Create();
  74. CkHttpResponse_GetBodySb(resp,sbResponseBody);
  75. jResp := CkJsonObject_Create();
  76. CkJsonObject_LoadSb(jResp,sbResponseBody);
  77. CkJsonObject_putEmitCompact(jResp,False);
  78.  
  79. Memo1.Lines.Add('Response Body:');
  80. Memo1.Lines.Add(CkJsonObject__emit(jResp));
  81.  
  82. respStatusCode := CkHttpResponse_getStatusCode(resp);
  83. Memo1.Lines.Add('Response Status Code = ' + IntToStr(respStatusCode));
  84. if (respStatusCode >= 400) then
  85.   begin
  86.     Memo1.Lines.Add('Response Header:');
  87.     Memo1.Lines.Add(CkHttpResponse__header(resp));
  88.     Memo1.Lines.Add('Failed.');
  89.     CkHttpResponse_Dispose(resp);
  90.     Exit;
  91.   end;
  92. CkHttpResponse_Dispose(resp);
  93.  
  94. // Sample JSON response:
  95. // (Sample code for parsing the JSON response is shown below)
  96.  
  97. // {
  98. //   "errorCode": "",
  99. //   "errorDescription": "",
  100. //   "requestID": "UICBY9QDLUOXQ72U"
  101. // }
  102.  
  103. // Sample code for parsing the JSON response...
  104. // Use the following online tool to generate parsing code from sample JSON:
  105. // Generate Parsing Code from JSON
  106.  
  107. errorCode := CkJsonObject__stringOf(jResp,'errorCode');
  108. errorDescription := CkJsonObject__stringOf(jResp,'errorDescription');
  109. requestID := CkJsonObject__stringOf(jResp,'requestID');
  110.  
  111. CkHttp_Dispose(http);
  112. CkStringBuilder_Dispose(sbSignedXml);
  113. CkJsonObject_Dispose(json);
  114. CkStringBuilder_Dispose(sbResponseBody);
  115. CkJsonObject_Dispose(jResp);
  116.  
  117. end;
  118.  

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1312
    • Lebeau Software
Re: Help to convert code in Indy
« Reply #1 on: November 28, 2022, 08:28:32 pm »
I ask you for help in converting this XML file submission procedure to lazarus using its components.

If you are asking how to rewrite that code to use TIdHTTP, then it would look something like this:

Code: Pascal  [Select][+][-]
  1. uses
  2.   Classes, IdHTTP, IdCoderMIME, fpjson, jsonparser;
  3.  
  4. procedure TForm1.Button1Click(Sender: TObject);
  5. var
  6.   http: TIdHTTP;
  7.   success: Boolean;
  8.   sSignedXml: string;
  9.   json: string;
  10.   sResponseBody: string;
  11.   respStatusCode: Integer;
  12.   jData: TJSONData;
  13.   jObject: TJSONObject;
  14.   errorCode: string;
  15.   errorDescription: string;
  16.   requestID: string;
  17.   FS: TFileStream;
  18.   SS: TStringStream;
  19. begin
  20.   http := TIdHTTP.Create;
  21.   try
  22.     // Implements the following CURL command:
  23.  
  24.     // curl -X POST https://auth.fatturazioneelettronica.aruba.it/services/ClientRequest/CreateTransmissionRequest \
  25.     //   -H "Accept: application/json" \
  26.     //   -H "Content-Type: application/json;charset=UTF-8" \
  27.     //   -d '{
  28.     //   "userName" : "ARUBA0000",
  29.     //   "password" : "ArubaPwd",
  30.     //   "userID" : "ARUBA0000",
  31.     //   "comunicationType" : "LI",
  32.     //   "dataFile" : "dGVzdA=="
  33.     // }'
  34.  
  35.     // Use the following online tool to generate HTTP code from a CURL command
  36.     // Convert a cURL Command to HTTP Source Code
  37.  
  38.     // Use this online tool to generate code from sample JSON:
  39.     // Generate Code to Create JSON
  40.  
  41.     // The following JSON is sent in the request body.
  42.  
  43.     // {
  44.     //   "userName": "ARUBA0000",
  45.     //   "password": "ArubaPwd",
  46.     //   "userID": "ARUBA0000",
  47.     //   "comunicationType": "LI",
  48.     //   "dataFile": "dGVzdA=="
  49.     // }
  50.  
  51.     // The dataFile field must contain an xml document which conforms to the Revenue Agency specifications.
  52.     // The document must be Base64 codified and contain a CADES-based electronic signature.
  53.  
  54.     FS := TFileStream.Create('IT01873080855_00310.xml', fmOpenRead or fmShareDenyWrite);
  55.     try
  56.       sSignedXml := TIdEncoderMIME.EncodeStream(FS);
  57.     finally
  58.       FS.Free;
  59.     end;
  60.  
  61.     jObject := TJSONObject.Create;
  62.     try
  63.       jObject.Add('userName', 'ARUBA0000');
  64.       jObject.Add('password', 'ArubaPwd');
  65.       jObject.Add('userID', 'ARUBA0000');
  66.       jObject.Add('comunicationType', 'LI');
  67.       jObject.Add('dataFile', sSignedXml);
  68.       json := jObject.AsJSON;
  69.     finally
  70.       jObject.Free;
  71.     end;
  72.  
  73.     http.Request.ContentType := 'application/json';
  74.     http.Request.CharSet := 'UTF-8';
  75.     http.Request.Accept := 'application/json';
  76.  
  77.     http.HTTPOptions := http.HTTPOptions + [hoNoProtocolErrorException, hoWantProtocolErrorContent];
  78.  
  79.     SS := TStringStream.Create(json);
  80.     try
  81.       sResponseBody := http.Post('https://ws.fatturazioneelettronica.aruba.it/services/ClientRequest/CreateTransmissionRequest', SS);
  82.     finally
  83.       SS.Free;
  84.     end;
  85.  
  86.     Memo1.Lines.Add('Response Body:');
  87.     Memo1.Lines.Add(sResponseBody);
  88.  
  89.     respStatusCode := http.Response.ResponseCode;
  90.     Memo1.Lines.Add('Response Status Code = ' + IntToStr(respStatusCode));
  91.     if (respStatusCode >= 400) then
  92.     begin
  93.       Memo1.Lines.Add('Response Header:');
  94.       Memo1.Lines.Add(http.Response.RawHeaders.Text);
  95.       Memo1.Lines.Add('Failed.');
  96.       Exit;
  97.     end;
  98.  
  99.     // Sample JSON response:
  100.     // (Sample code for parsing the JSON response is shown below)
  101.  
  102.     // {
  103.     //   "errorCode": "",
  104.     //   "errorDescription": "",
  105.     //   "requestID": "UICBY9QDLUOXQ72U"
  106.     // }
  107.  
  108.     // Sample code for parsing the JSON response...
  109.     // Use the following online tool to generate parsing code from sample JSON:
  110.     // Generate Parsing Code from JSON
  111.  
  112.     jData := GetJSON(sResponseBody);
  113.     try
  114.       jObject := jData as TJSONObject;
  115.       errorCode := jObject.Get('errorCode');
  116.       errorDescription := jObject.Get('errorDescription');
  117.       requestID := jObject.Get('requestID');
  118.     finally
  119.       jData.Free;
  120.     end;
  121.   finally
  122.     http.Free;
  123.   end;
  124. end;
« Last Edit: November 30, 2022, 12:02:18 am by Remy Lebeau »
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

laguna

  • Sr. Member
  • ****
  • Posts: 323
Re: Help to convert code in Indy
« Reply #2 on: November 29, 2022, 01:44:53 pm »
it's work, now implement program and i will publish in github

thanks a lot


 

TinyPortal © 2005-2018