Forum > Networking and Web Programming

Help to convert code in Indy

(1/1)

laguna:
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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} --- procedure TForm1.Button1Click(Sender: TObject);varhttp: HCkHttp;success: Boolean;sbSignedXml: HCkStringBuilder;json: HCkJsonObject;resp: HCkHttpResponse;sbResponseBody: HCkStringBuilder;jResp: HCkJsonObject;respStatusCode: Integer;errorCode: PWideChar;errorDescription: PWideChar;requestID: PWideChar; begin// This example assumes the Chilkat API to have been previously unlocked.// See Global Unlock Sample for sample code. http := CkHttp_Create(); // Implements the following CURL command: // curl -X POST https://auth.fatturazioneelettronica.aruba.it/services/ClientRequest/CreateTransmissionRequest \//   -H "Accept: application/json" \//   -H "Content-Type: application/json;charset=UTF-8" \//   -d '{//   "userName" : "ARUBA0000",//   "password" : "ArubaPwd",//   "userID" : "ARUBA0000",//   "comunicationType" : "LI",//   "dataFile" : "dGVzdA=="// }' // Use the following online tool to generate HTTP code from a CURL command// Convert a cURL Command to HTTP Source Code // Use this online tool to generate code from sample JSON:// Generate Code to Create JSON // The following JSON is sent in the request body. // {//   "userName": "ARUBA0000",//   "password": "ArubaPwd",//   "userID": "ARUBA0000",//   "comunicationType": "LI",//   "dataFile": "dGVzdA=="// } // The dataFile field must contain an xml document which conforms to the Revenue Agency specifications.// The document must be Base64 codified and contain a CADES-based electronic signature.sbSignedXml := CkStringBuilder_Create();success := CkStringBuilder_LoadFile(sbSignedXml,'IT01873080855_00310.xml','utf-8'); json := CkJsonObject_Create();CkJsonObject_UpdateString(json,'userName','ARUBA0000');CkJsonObject_UpdateString(json,'password','ArubaPwd');CkJsonObject_UpdateString(json,'userID','ARUBA0000');CkJsonObject_UpdateString(json,'comunicationType','LI');CkJsonObject_UpdateString(json,'dataFile',CkStringBuilder__getEncoded(sbSignedXml,'base64','utf-8')); CkHttp_SetRequestHeader(http,'Content-Type','application/json;charset=UTF-8');CkHttp_SetRequestHeader(http,'Accept','application/json'); resp := CkHttp_PostJson3(http,'https://ws.fatturazioneelettronica.aruba.it/services/ClientRequest/CreateTransmissionRequest','application/json',json);if (CkHttp_getLastMethodSuccess(http) = False) then  begin    Memo1.Lines.Add(CkHttp__lastErrorText(http));    Exit;  end; sbResponseBody := CkStringBuilder_Create();CkHttpResponse_GetBodySb(resp,sbResponseBody);jResp := CkJsonObject_Create();CkJsonObject_LoadSb(jResp,sbResponseBody);CkJsonObject_putEmitCompact(jResp,False); Memo1.Lines.Add('Response Body:');Memo1.Lines.Add(CkJsonObject__emit(jResp)); respStatusCode := CkHttpResponse_getStatusCode(resp);Memo1.Lines.Add('Response Status Code = ' + IntToStr(respStatusCode));if (respStatusCode >= 400) then  begin    Memo1.Lines.Add('Response Header:');    Memo1.Lines.Add(CkHttpResponse__header(resp));    Memo1.Lines.Add('Failed.');    CkHttpResponse_Dispose(resp);    Exit;  end;CkHttpResponse_Dispose(resp); // Sample JSON response:// (Sample code for parsing the JSON response is shown below) // {//   "errorCode": "",//   "errorDescription": "",//   "requestID": "UICBY9QDLUOXQ72U"// } // Sample code for parsing the JSON response...// Use the following online tool to generate parsing code from sample JSON:// Generate Parsing Code from JSON errorCode := CkJsonObject__stringOf(jResp,'errorCode');errorDescription := CkJsonObject__stringOf(jResp,'errorDescription');requestID := CkJsonObject__stringOf(jResp,'requestID'); CkHttp_Dispose(http);CkStringBuilder_Dispose(sbSignedXml);CkJsonObject_Dispose(json);CkStringBuilder_Dispose(sbResponseBody);CkJsonObject_Dispose(jResp); end; 

Remy Lebeau:

--- Quote from: laguna 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.

--- End quote ---

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


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---uses  Classes, IdHTTP, IdCoderMIME, fpjson, jsonparser; procedure TForm1.Button1Click(Sender: TObject);var  http: TIdHTTP;  success: Boolean;  sSignedXml: string;  json: string;  sResponseBody: string;  respStatusCode: Integer;  jData: TJSONData;  jObject: TJSONObject;  errorCode: string;  errorDescription: string;  requestID: string;  FS: TFileStream;  SS: TStringStream;begin  http := TIdHTTP.Create;  try    // Implements the following CURL command:     // curl -X POST https://auth.fatturazioneelettronica.aruba.it/services/ClientRequest/CreateTransmissionRequest \    //   -H "Accept: application/json" \    //   -H "Content-Type: application/json;charset=UTF-8" \    //   -d '{    //   "userName" : "ARUBA0000",    //   "password" : "ArubaPwd",    //   "userID" : "ARUBA0000",    //   "comunicationType" : "LI",    //   "dataFile" : "dGVzdA=="    // }'     // Use the following online tool to generate HTTP code from a CURL command    // Convert a cURL Command to HTTP Source Code     // Use this online tool to generate code from sample JSON:    // Generate Code to Create JSON     // The following JSON is sent in the request body.     // {    //   "userName": "ARUBA0000",    //   "password": "ArubaPwd",    //   "userID": "ARUBA0000",    //   "comunicationType": "LI",    //   "dataFile": "dGVzdA=="    // }     // The dataFile field must contain an xml document which conforms to the Revenue Agency specifications.    // The document must be Base64 codified and contain a CADES-based electronic signature.     FS := TFileStream.Create('IT01873080855_00310.xml', fmOpenRead or fmShareDenyWrite);    try      sSignedXml := TIdEncoderMIME.EncodeStream(FS);    finally      FS.Free;    end;     jObject := TJSONObject.Create;    try      jObject.Add('userName', 'ARUBA0000');      jObject.Add('password', 'ArubaPwd');      jObject.Add('userID', 'ARUBA0000');      jObject.Add('comunicationType', 'LI');      jObject.Add('dataFile', sSignedXml);      json := jObject.AsJSON;    finally      jObject.Free;    end;     http.Request.ContentType := 'application/json';    http.Request.CharSet := 'UTF-8';    http.Request.Accept := 'application/json';     http.HTTPOptions := http.HTTPOptions + [hoNoProtocolErrorException, hoWantProtocolErrorContent];     SS := TStringStream.Create(json);    try      sResponseBody := http.Post('https://ws.fatturazioneelettronica.aruba.it/services/ClientRequest/CreateTransmissionRequest', SS);    finally      SS.Free;    end;     Memo1.Lines.Add('Response Body:');    Memo1.Lines.Add(sResponseBody);     respStatusCode := http.Response.ResponseCode;    Memo1.Lines.Add('Response Status Code = ' + IntToStr(respStatusCode));    if (respStatusCode >= 400) then    begin      Memo1.Lines.Add('Response Header:');      Memo1.Lines.Add(http.Response.RawHeaders.Text);      Memo1.Lines.Add('Failed.');      Exit;    end;     // Sample JSON response:    // (Sample code for parsing the JSON response is shown below)     // {    //   "errorCode": "",    //   "errorDescription": "",    //   "requestID": "UICBY9QDLUOXQ72U"    // }     // Sample code for parsing the JSON response...    // Use the following online tool to generate parsing code from sample JSON:    // Generate Parsing Code from JSON     jData := GetJSON(sResponseBody);    try      jObject := jData as TJSONObject;      errorCode := jObject.Get('errorCode');      errorDescription := jObject.Get('errorDescription');      requestID := jObject.Get('requestID');    finally      jData.Free;    end;  finally    http.Free;  end;end;

laguna:
it's work, now implement program and i will publish in github

thanks a lot

Navigation

[0] Message Index

Go to full version