Skip to content

Instantly share code, notes, and snippets.

@Ergin008
Created December 28, 2012 00:39
Show Gist options
  • Save Ergin008/4393518 to your computer and use it in GitHub Desktop.
Save Ergin008/4393518 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
#
# API WALKTHROUGH #4 in Perl - Request Signature on a Document
#
use strict;
use LWP 6.00;
use JSON;
$ENV{PERL_LWP_SSL_VERIFY_HOSTNAME}=0;
# Enter your info:
my $username = "***";
my $password = "***";
my $integratorKey = "***";
my $filename = 'test.pdf'; #change to your document name
my $authHeader = '<DocuSignCredentials><Username>' . $username . '</Username>' .
'<Password>' . $password . '</Password>' .
'<IntegratorKey>' . $integratorKey . '</IntegratorKey></DocuSignCredentials>';
######################################################################
# STEP 1 - Login
######################################################################
my $url = 'https://demo.docusign.net/restapi/v2/login_information';
my $browser = LWP::UserAgent->new;
my @requestHeaders = (
'X-DocuSign-Authentication' => $authHeader,
'Content-Type' => 'application/json',
'Accept' => 'application/json');
my $response = $browser->get( $url, @requestHeaders );
die "Error encountered during login request -- ", $response->status_line . "\n"
unless $response->is_success;
my $decoded_json = decode_json( $response->content );
my $baseUrl = $decoded_json->{"loginAccounts"}[0]->{"baseUrl"};
my $accountId = $decoded_json->{"loginAccounts"}[0]->{"accountId"};
#-- display results
print "\nLogin successful...\nbaseUrl = " . $baseUrl . "\naccountId = " . $accountId . "\n";
######################################################################
# STEP 2 - Request Signature on Document
######################################################################
# append "/envelopes" to $baseUrl and use in the next request
$url = $baseUrl . "/envelopes";
# construct the json formatted part of the request body
my %jsonBody = (
'emailSubject' => 'Signature Request Document',
'emailBlurb' => 'This comes from Perl',
'documents' => [{
'documentId' => '1',
'name' => 'test.pdf'
}],
'recipients' => {
'signers' => [{
'email' => $username,
'name' => 'John Doe',
'recipientId' => '1'}]
},
'tabs' => {
'signHereTabs' => [{
'xPosition' => '100',
'yPosition' => '100',
'documentId' => '1',
'pageNumber' => '1'}]},
'status' => 'sent'
);
my $jsonEncoded = encode_json \%jsonBody;
my $fileContents;
# read the document bytes into a perl scalar, then append to the request body
my $content;
open(my $fh, '<', $filename) or die "Error opening file $filename, quiting program.";
{
local $/;
$fileContents = <$fh>;
}
close($fh);
# build the multipart request body
my $requestBody = "\r\n\r\n--BOUNDARY\r\n" .
"Content-Type: application/json\r\n" .
"Content-Disposition: form-data\r\n" .
"\r\n" .
$jsonEncoded . "\r\n\r\n--BOUNDARY\r\n" .
"Content-Type: application/pdf\r\n" .
"Content-Disposition: file; filename=\"test.pdf\"; documentId=1\r\n" .
"\r\n" .
$fileContents . "\r\n" .
"--BOUNDARY--\r\n\r\n";
@requestHeaders = (
'X-DocuSign-Authentication' => $authHeader,
'Content-Type' => 'multipart/form-data;boundary=BOUNDARY',
'Accept' => 'application/json');
my $req = HTTP::Request->new(POST => $url);
$req->header( @requestHeaders );
$req->content( $requestBody );
$response = $browser->request( $req );
die "Error encountered during login request -- ", $response->content #$response->status_line
unless $response->is_success;
$decoded_json = decode_json( $response->content );
my $envelopeId = $decoded_json->{"envelopeId"};
#-- display results
print "\nDocument sent! EnvelopeId = $envelopeId\n\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment