Metilda has asked for the wisdom of the Perl Monks concerning the following question:

Please help to convert this C# code to perl

var client = new RestClient("url"); var request = new RestRequest(Method.PUT); // Create SSL certificate // Certificate CN name is used to authenticate with Secure Gateway string certificateFile = "<SSL_Certificate>.pfx"; var certificate = new X509Certificate(certificateFile); client.ClientCertificates = new X509CertificateCollection(); client.ClientCertificates.Add(certificate);

Replies are listed 'Best First'.
Re: convert C# to perl script
by Corion (Patriarch) on Sep 18, 2018 at 07:58 UTC

    Hello Metilda!

    What are your problems with converting the C# code to Perl? Are you looking for the right modules to use? Are you looking for a general approach of converting C# to Perl?

    What Perl code have you already written for this problem and how does it fail to do what you need?

      Hi, I have installed REST::Client and Crypt::X509 modules.

      my $url = "https://my rest services url"; my $jsonBody = '{ json details }'; my $client = REST::Client->new(); $client->addHeader('Content-Type', 'application/json'); $client->addHeader('charset', 'UTF-8'); $client->addHeader('Accept', 'application/json'); $client->addHeader('Authorization', 'Basic xxxxxxxxxxxxxxxxxxxxxxx'); $client->PUT($url, $jsonBody);
      The above script is working fine without using ssl certificate. Now i want to include ssl certificate. I have my pfx file and want to create certificate object using X509Certificate. Need your help to convert that C# code to perl.

        I have my pfx file and want to create certificate

        You could do this separately using a CLI utility such as openssl but if you want to do that inside perl, take a look at Crypt::OpenSSL::PKCS12 which shows in the synopsis how to achieve this.

        Looking through the documentation for Rest::Client, I find these two lines:

        #X509 client authentication $client->setCert('/path/to/ssl.crt'); $client->setKey('/path/to/ssl.key');

        Have you tried adding your certificate and key file that way? How does it fail?