After some trial and error again, was able to figure it out. This integration is with the Datylon API & Delphi REST components which handles a PDF return type. Although free account in Datylon doesn't work with this API integration. If anyone want to implements the API or something similar, the following code can be utilized. You just need to add some error handling.
Getauthorization - a function that return the Basic authentication with username and password 64bit encoded, small letter 'a' in 'authorization' is from datylon documentation
templateid and datasource - these are datylon parameters that needs be followed based on Datylon documentation
JSONStr - this is actually the JSON string that needs to be sent
TRY
RESTRequest := TRESTRequest.create(nil);
RESTRequest.Client := TRESTClient.create(RESTRequest);
RESTRequest.SynchronizedEvents := False;
RESTRequest.Client.UserAgent := DatylonUserAgent;
RESTRequest.Client.FallbackCharsetEncoding := 'raw';
RESTRequest.Client.BaseURL := datylon_BaseURI;
RESTRequest.Client.ContentType := 'application/json';
RESTRequest.Client.Params.AddItem('authorization', Getauthorization, TRESTRequestParameterkind.pkHTTPHEADER, [TRESTRequestParameterOption.poDoNotEncode]);
RESTRequest.Client.Params.AddItem('x-graph-template-id', templateid, TRESTRequestParameterkind.pkHTTPHEADER, [TRESTRequestParameterOption.poDoNotEncode]);
RESTRequest.Client.Params.AddItem('x-graph-datasource-name', datasource, TRESTRequestParameterkind.pkHTTPHEADER, [TRESTRequestParameterOption.poDoNotEncode]);
RESTRequest.Body.ClearBody;
RestRequest.body.Add(JSONStr, ctAPPLICATION_JSON);
// RESTRequest.Resource := 'render';
RESTRequest.Method := rmPOST;
RESTRequest.Response := TRESTResponse.Create(RESTRequest);
RESTRequest.Accept := 'application/pdf';
RESTRequest.Execute;
Codesite.Send('AuthenticationProcess Result: ' + RESTRequest.Response.Content);
Result := RESTRequest.Response.RawBytes;
finally
RESTRequest.Free;
end;
This code result will return rawbytes which you need to convert to a file. You need the FallbackCharsetEncoding to 'raw' - this I'm not sure but I've read this from here: https://community.embarcadero.com/de/component/easydiscuss/rest-request-error-to-get-pdf-attached-berlin-10-1?Itemid=1 I'm not even sure if it made a difference but I lost track of it which ones is working and which one is not when I was doing my trial and error. 🤣 the ultimate goal is to make it work haha
This is for the bytes to file conversion:
procedure TfrmMain.SaveBytesToFile(const Data: TBytes; const FileName: string);
var
Stream: TFileStream;
begin
Stream := TFileStream.Create(FileName, fmCreate);
try
if Data <> nil then
Stream.WriteBuffer(Data[0], Length(Data));
finally
Stream.Free;
end;
end;
//This will call the REST function above
RestResultFile := API.PDFProcess(LoadTextFromFile('text1.txt')); //text file here contains the JSON string
SaveBytesToFile(RestResultFile, 'test.pdf');
Thank you guys for your ideas. It led me to this solution above. Cheers!