POST/get JSON using Ararat Synapse with special PEM key (Lazarus/FPC)

595 views Asked by At

We are forced to send (and receive back) JSON data to the government from our restaurant program

  • using a special RSA Key (PEM stream) they provide
  • using TLS 1.3 (OpenSSL 3.0+) < This works! :-)

The only way I found is to create a separate DLL for that under Lazarus, which can use a separate component to do that, (and load that special key, so it does not interfere with the main program's Indy component.)

Ararat Synapse's new Trunk (R260) can handle OpenSSL 3.0, so I've downloaded and installed as component, but the JSON I'm sending to a test server (webhook.com) are distorted krix-krax.

How do I load a special .PEM key/stream (not a file!) for the SSL communication before sending it?

Here is my code:

function HttpPostGetJSON(const URL: string; var JSON: UTF8String; const key, pem: string; const TimeOut: integer = 5000): Boolean;
var
    HTTP    : THTTPSend;
    Data    : TStringStream;
begin
    HTTP := THTTPSend.Create;
    Data := TStringStream.Create(JSON, TEncoding.UTF8);
    JSON := '';
    HTTP.Timeout := TimeOut;           

    try
        HTTP.Headers.Add('Content-Type: application/json; charset=UTF-8') ;
        HTTP.Headers.Add('Accept: application/json') ;

        // TODO: specify PEM key for the SSL !

        Data.Position := 0;
        HTTP.Document.CopyFrom( Data, 0); 
        Data.Size := 0;

        Result := HTTP.HTTPMethod('POST', URL);
        if Result then begin
            if HTTP.Document.Size > 0 then begin
                Data.LoadFromStream( HTTP.Document );
                JSON := Data.UnicodeDataString;  
            end;
        end;
    finally
        HTTP.Free;
        Data.Free;
    end;
end;
0

There are 0 answers