Code Snippets (Perl)

This sections contains code snippets covering the examples provided in the WebDAV API section. Only the relevant lines of code that relate to the request have been included. For a full example see: Simple HTTP Request

CHECKIN

Example 1: A successful CHECKIN  request

my $url = "http://${server}/Example/Files/test.txt";
my $requestObj = new HTTP::Request('CHECKIN', "${url}"); 

CHECKOUT

Example 1: A successful CHECKOUT  request

my $url = "http://${server}/Example/Files/test.txt";
my $requestObj = new HTTP::Request('CHECKOUT', "${url}");

COPY

Example 1: Copying a Collection

my $url = "http://${server}/Example/Folders/";
my $requestObj = new HTTP::Request('COPY', "${url}");
$requestObj->header("Destination" => "http://${server}/Copy/");
$requestObj->header("Overwrite" => "F");

Example 2: Overwriting a Collection

my $url = "http://${server}/Example/Folders/";
my $requestObj = new HTTP::Request('COPY', "${url}");
$requestObj->header("Destination" => "http://${server}/Copy/");
$requestObj->header("Overwrite" => "T");

Example 3: Copying a File with no overwrite

my $url = "http://${server}/Example/Folders/howto.txt";
my $requestObj = new HTTP::Request('COPY', "${url}");
$requestObj->header("Destination" => "http://${server}/Copy/howto.txt");
$requestObj->header("Overwrite" => "F");

DELETE

Example 1: Successful DELETE

my $url = "http://${server}/Example/Files/";
my $requestObj = new HTTP::Request('DELETE', "${url}");

GET / HEAD

Example 1: GET

my $url = "http://${server}/Example/Files/getExample.txt?version=1";
my $requestObj = my $request = new HTTP::Request('GET', "${url}");

Example 2: HEAD

my $url = "http://${server}/Example/Files/getExample.txt?version=1";
my $requestObj = my $request = new HTTP::Request('HEAD', "${url}");

MKCOL

Example 1: Creating a new Collection

my $url = "http://${server}/Example/Files/Test/";
my $requestObj = new HTTP::Request('MKCOL', "${url}");

MOVE

Example 1: Moving a Collection

my $url = "http://${server}/Example/";
my $requestObj = new HTTP::Request('MOVE', "${url}");
$requestObj->header("Depth" => "infinity" );
$requestObj->header("Destination" => "http://${server}/Copy/");
$requestObj->header("Overwrite" => "F");

Example 2: Moving a Document

my $url = "http://${server}/Example/Move.txt";
my $requestObj = new HTTP::Request('MOVE', "${url}");
$requestObj->header("Depth" => "infinity" );
$requestObj->header("Destination" => "http://${server}/Copy/Move.txt");
$requestObj->header("Overwrite" => "F"); 

OPTIONS

Example 1: OPTIONS on a resource

my $url = "http://${server}/Example/Files/";
my $requestObj = new HTTP::Request('OPTIONS', "${url}");

PROPFIND

Example 1: PROPFIND with no message body

my $url = "http://${server}/Example/Files/";
my $requestObj = new HTTP::Request('PROPFIND', "${url}");
$requestObj->header( "Depth" => "0" );

Example 2: Returning a named property

my $url = "http://${server}/Example/Files/";
my $requestMessage =
 "<?xml version='1.0' encoding='utf-8' ?>
  <D:propfind xmlns:D='DAV:'>
    <D:prop xmlns=''>
      <D:displayname/>
    </D:prop>
  </D:propfind>";
my $requestObj = new HTTP::Request('PROPFIND', "${url}");
$requestObj->header( "Depth" => "1" );
$requestObj->content( $requestMessage );

Example 3: Using propname

my $url = "http://${server}/Example/Files/howto.txt";
my $requestMessage =
 "<?xml version='1.0' encoding='utf-8' ?>
  <D:propfind xmlns:D='DAV:'>
    <D:propname/>
  </D:propfind>";
my $requestObj = new HTTP::Request('PROPFIND', "${url}");
$requestObj->content( $requestMessage );

Example 4: Working with revisions

my $url = "http://${server}/Example/Files/howto.txt?version=2";
my $requestMessage =
 "<?xml version='1.0' encoding='utf-8' ?>
  <D:propfind xmlns:D='DAV:'>
    <D:prop xmlns=''>
      <D:comment/>
    </D:prop>
  </D:propfind>";
my $requestObj = new HTTP::Request('PROPFIND', "${url}");
$requestObj->content( $requestMessage );

 

PROPPATCH

Example 1: Altering a property value

my $url = "http://${server}/Example/Files/";
my $requestMessage =
  "<?xml version='1.0' encoding='utf-8'?>
<propertyupdate xmlns='DAV:'>
  <set>
    <prop>
      <displayname>New Displayname</displayname>     
    </prop>
  </set>
</propertyupdate>
";
my $requestObj = new HTTP::Request('PROPPATCH', "${url}");
$requestObj->content( $requestMessage );

Example 2: Removing a property

my $url = "http://${server}/Example/Files/";
my $requestMessage =
  "<?xml version='1.0' encoding='utf-8'?>
<propertyupdate xmlns='DAV:' xmlns:Z='www.docmoto.com/ns/2006/docmoto/'>
  <remove>
    <prop>
      <Z:file-created>
    </prop>
  </remove>
</propertyupdate>";
my $requestObj = new HTTP::Request('PROPPATCH', "${url}");
$requestObj->content( $requestMessage );

Example 3: Adding a property

my $url = "http://${server}/Example/Files/";
my $requestMessage =
  "<?xml version='1.0' encoding='utf-8'?>
  <propertyupdate xmlns='DAV:'>
  <set>
    <prop>
      <exampleproperty>Property Data</exampleproperty>    
    </prop>
  </set>
</propertyupdate>";
my $requestObj = new HTTP::Request('PROPPATCH', "${url}");
$requestObj->content( $requestMessage );

PUT

Example 1: Using PUT to create a new Document (combined with VERSION-CONTROL)

my $url = "http://${server}/Example/Files/NewFile.txt";
my $requestObj = new HTTP::Request('PUT', "${url}");
$requestObj->content("This is the contents of the new file");
.....
$requestObj = new HTTP::Request('VERSION-CONTROL', "${url}");


Example 2
: Using PUT to create a new Revision (combined with CHECKOUT/CHECKIN)

my $url = "http://${server}/Example/Files/Elijah.txt";
my $requestObj = new HTTP::Request('CHECKOUT', "${url}");
......
$requestObj = new HTTP::Request('PUT', "${url}");
$requestObj->content( "New contents of the file" );
......
$requestObj = new HTTP::Request('CHECKIN', "${url}");

REPORT

Example 1: Returning a list of versions

my $url = "http://${server}/Example/Files/howto.txt";
my $requestMessage =
  "<?xml version='1.0' encoding='utf-8' standalone='no' ?>
<version-tree xmlns='DAV:'/>";
my $requestObj = new HTTP::Request('REPORT', "${url}");
$requestObj->headers("Depth"=>"infinity");
$requestObj->content( $requestMessage );

Example 2: Returning named properties

my $url = "http://${server}/Example/Files/howto.txt";
my $requestObj = new HTTP::Request('REPORT', "${url}");
$requestObj->headers("Depth"=>"infinity");
$requestObj->content(
"<?xml version='1.0' encoding='utf-8' standalone='no' ?>
<D:version-tree xmlns:D='DAV:'>
  <D:prop>
    <D:comment/>
  </D:prop>
</D:version-tree>
");

SEARCH

Example 1: Searching for a string

 

UNCHECKOUT

Example 1: Successful UNCHECKOUT request (Combined with CHECKOUT)

my $url = "http://${server}/Example/Files/test.txt";

my $requestObj = new HTTP::Request('CHECKOUT', "${url}");
....

$requestObj = new HTTP::Request('UNCHECKOUT', "${url}");

VERSION-CONTROL

Example 1: Successful VERSION-CONTROL request

my $url = "http://${server}/Example/Files/howto.txt";
$requestObj = new HTTP::Request('VERSION-CONTROL', "${url}");

Copyright 2013 CHLSoftware. All rights reserved. All trademarks acknowledged.