webdav API reference
A WebDAV client (RFC 4918): list, stat, read, write, create, move, copy and delete files on any WebDAV server (Nextcloud, ownCloud, Apache mod_dav, nginx, rclone, a NAS), plus propfind / proppatch for any property.
The module has two layers. The protocol layer (Resource, paths and hrefs, the multistatus codec, Clark notation for property names) knows nothing about sockets, so a server built on httpd can share it: it encodes the same Resource the client decodes. The client sends the requests over http and keeps the session in a value-semantic Client. Needs the default jennifer binary (net via http).
Every failure throws a webdav.WebdavError (kind "webdav") with the request's method, path and refused status (412 for a failed ifMatch, 423 locked, ...); for a 207 Multi-Status reply, the first failed member. The generic kinds go on: a socket failure is http's net.NetError (kind "net"), a reached cap kind "limit", a server that stops answering "timeout" and a reply cut short "closed".
A credential (an Authorization or Cookie header) goes only over https://, or http:// to a loopback host; any other http:// URL throws before the request is sent, unless the client sets allowInsecureHttp.
Import with import "webdav.j" as webdav;. See the webdav guide for prose and examples.
Contents
| Name | What it is |
|---|---|
clark | A property name in Clark notation, as Resource.props, propfind and proppatch take it. |
cleanPath | A path checked and normalized: a leading /, no empty, . or .. segment, no control character, no trail... |
connect | A client for a WebDAV server below a URL, with no credential yet. |
copy | Copy a file, or a collection with everything in it. |
decodeHref | The decoded server path of an href, which a server may send as a path or as a full URL. |
delete | Delete a file, or a collection with everything in it. |
encodeHref | A decoded path as an href: each segment percent-encoded. |
exists | Whether a path exists. |
get | Read a file. |
mkdir | Create a collection. |
mkdirAll | Create a collection and any missing parents. |
move | Move or rename a file or collection. |
multistatus | Encode resources as a 207 Multi-Status body: what a server answers a PROPFIND, and the inverse of... |
parseMultistatus | Decode a 207 Multi-Status body into its resources. |
propfind | Ask for properties of a path, and of its members with depth 1. |
proppatch | Set or remove properties of a path, all or none. |
put | Write a file, replacing it when it exists. |
putWith | Write a file with a media type or a condition. |
readDir | The members of a collection, without the collection itself. |
stat | One file or collection. |
walk | Everything below a collection, collections before their members. |
withBasicAuth | Authenticate with a user name and password (HTTP Basic). |
withCA | Trust a certificate (PEM) instead of the system roots. |
withHeader | Send a header with every request, such as OCS-APIRequest: true. |
withSkipVerify | Accept any certificate. |
withTimeout | Set the idle timeout of each request. |
withToken | Authenticate with a bearer token. |
Client | struct: A connection to a WebDAV server below a base URL. |
PutOptions | struct: How putWith writes a file. |
Resource | struct: A file or collection and its properties, as a PROPFIND reports it (and as a server answers one). |
WebdavError | struct: A failed WebDAV operation. |
| constants | the module's constants |
Functions
clark
webdav.clark(ns as string, local as string)A property name in Clark notation, as Resource.props, propfind and proppatch take it.
ns- the namespace URI, e.g.
DAV: local- the local name, e.g.
getetag - Returns
string - e.g.
{DAV:}getetag
cleanPath
webdav.cleanPath(path as string)A path checked and normalized: a leading /, no empty, . or .. segment, no control character, no trailing /.
path- e.g.
/Documents/a.txt; "" or/for the root - Returns
string - the path;
/for the root
connect
webdav.connect(url as string)A client for a WebDAV server below a URL, with no credential yet.
urlhttp://orhttps://, e.g.https://cloud.example.com/remote.php/dav/files/alice- Returns
Client - the client; 30 s idle timeout, full certificate checking
- Throws
WebdavError - kind
"webdav"on a URL that is not http(s) or carries credentials (usewithBasicAuth), a query or a fragment
copy
webdav.copy(
c as Client,
source as string,
target as string,
overwrite as bool)Copy a file, or a collection with everything in it.
c- the client
source- e.g.
/Templates/report.odt target- e.g.
/Documents/report.odt overwrite- replace an existing destination
- Throws
WebdavError - kind
"webdav"asmove
decodeHref
webdav.decodeHref(href as string)The decoded server path of an href, which a server may send as a path or as a full URL.
href- e.g.
/a%20b/orhttps://host/a%20b/ - Returns
string - the decoded path, e.g.
/a b/
delete
webdav.delete(c as Client, path as string)Delete a file, or a collection with everything in it.
c- the client
path- e.g.
/Documents/old - Throws
WebdavError - kind
"webdav"on the root, when it does not exist (HTTP 404), or when the server cannot delete all of it (HTTP 207, naming the first member it kept)
encodeHref
webdav.encodeHref(path as string, isDir as bool)A decoded path as an href: each segment percent-encoded.
path- a clean path
isDir- add the trailing
/of a collection - Returns
string - the href, e.g.
/My%20Files/
exists
webdav.exists(c as Client, path as string)Whether a path exists.
c- the client
path- e.g.
/Documents/a.txt - Returns
bool - whether it does
- Throws
WebdavError - kind
"webdav"on a bad path or any answer but 207 and 404
get
webdav.get(c as Client, path as string, maxBytes as int)Read a file.
c- the client
path- e.g.
/Documents/a.txt maxBytes- the largest file accepted; 0 for 64 MiB,
math.MAX_INTfor no limit - Returns
bytes - its content
- Throws
WebdavError - kind
"webdav"when it does not exist or the server refuses, or for a negativemaxBytes - Throws
Error - kind
"limit"when it is larger thanmaxBytes
mkdir
webdav.mkdir(c as Client, path as string)Create a collection. Its parent must exist.
c- the client
path- e.g.
/Documents/2026 - Throws
WebdavError - kind
"webdav"when it exists (HTTP 405), the parent does not (HTTP 409), or the server refuses
mkdirAll
webdav.mkdirAll(c as Client, path as string)Create a collection and any missing parents. An existing one is no error.
c- the client
path- e.g.
/Backups/2026/11 - Throws
WebdavError - kind
"webdav"when a part of the path is a file, or the server refuses
move
webdav.move(
c as Client,
source as string,
target as string,
overwrite as bool)Move or rename a file or collection.
c- the client
source- e.g.
/Inbox/scan.pdf target- e.g.
/Documents/scan.pdf overwrite- replace an existing destination
- Throws
WebdavError - kind
"webdav"when the destination exists andoverwriteis false (HTTP 412), its parent does not (HTTP 409), or the server refuses
multistatus
webdav.multistatus(resources as list of Resource)Encode resources as a 207 Multi-Status body: what a server answers a PROPFIND, and the inverse of parseMultistatus.
resources- the resources;
hrefis their server path, decoded; each entry ofpropsis written as text - Returns
string - the XML, with its declaration
parseMultistatus
webdav.parseMultistatus(body as string)Decode a 207 Multi-Status body into its resources. Properties a server reports with a non-2xx status are left out, and so are responses that are a status only (404 for a member that vanished).
body- the XML
- Returns
list of Resource - the resources, in the order of the reply;
pathis "" (the client fills it in) - Throws
WebdavError - kind
"webdav"when the body is not aDAV:multistatus, a response has noDAV:hrefor neither aDAV:propstatnor aDAV:status, a status line is malformed, or a size, quota or date is not one - Throws
Error - kind
"limit"for a body pastxml's decoding caps
propfind
webdav.propfind(
c as Client,
path as string,
depth as int,
props as list of string)Ask for properties of a path, and of its members with depth 1.
c- the client
path- e.g.
/Documents depth- 0 for the path, 1 for it and its members
props- Clark names, e.g.
{http://owncloud.org/ns}fileid;[]for all the server offers - Returns
list of Resource - the path first, then its members
- Throws
WebdavError - kind
"webdav"on a bad path, depth or name, or when the server refuses
proppatch
webdav.proppatch(
c as Client,
path as string,
set as map of string to string,
remove as list of string)Set or remove properties of a path, all or none.
c- the client
path- e.g.
/Documents/a.txt set- text properties to set, by Clark name, e.g.
{http://owncloud.org/ns}favorite: "1" remove- properties to remove, by Clark name
- Throws
WebdavError - kind
"webdav"on a bad name, or when the server refuses a property (naming it)
put
webdav.put(c as Client, path as string, data as bytes)Write a file, replacing it when it exists.
c- the client
path- e.g.
/Documents/a.txt; its collection must exist data- the content
- Returns
string - the new entity tag when the server sends one; "" otherwise
- Throws
WebdavError - kind
"webdav"when the collection does not exist (HTTP 409), or the server refuses
putWith
webdav.putWith(
c as Client,
path as string,
data as bytes,
options as PutOptions)Write a file with a media type or a condition.
c- the client
path- e.g.
/Documents/a.txt data- the content
options- the media type, and
ifMatch/onlyIfNew - Returns
string - the new entity tag when the server sends one; "" otherwise
- Throws
WebdavError - kind
"webdav"when a condition fails (HTTP 412), on a weakifMatchtag, or asput
readDir
webdav.readDir(c as Client, path as string)The members of a collection, without the collection itself.
c- the client
path- e.g.
/Documents;/for the client's URL - Returns
list of Resource - the members, in the server's order
- Throws
WebdavError - kind
"webdav"when it is a file, does not exist, the server lists a path that is not a direct member, or the server refuses
stat
webdav.stat(c as Client, path as string)One file or collection.
c- the client
path- e.g.
/Documents/a.txt - Returns
Resource - it
- Throws
WebdavError - kind
"webdav"when it does not exist (HTTP 404), or when the server refuses
walk
webdav.walk(c as Client, path as string)Everything below a collection, collections before their members. It asks with depth 1 per collection, since many servers refuse Depth: infinity.
c- the client
path- the collection
- Returns
list of Resource - the files and collections below it
- Throws
Error - kind
"limit"pastMAX_WALKentries - Throws
WebdavError - kind
"webdav"asreadDir
withBasicAuth
webdav.withBasicAuth(c as Client, user as string, password as string)Authenticate with a user name and password (HTTP Basic). They travel readable, so a request over http:// to a host other than loopback throws unless the client sets allowInsecureHttp.
c- the client
user- the user name, without
: password- the password; an app password where the server offers them
- Returns
Client - the changed copy
- Throws
WebdavError - kind
"webdav"on a:in the name or a control character
withCA
webdav.withCA(c as Client, pem as bytes)Trust a certificate (PEM) instead of the system roots.
c- the client
pem- the certificate
- Returns
Client - the changed copy
withHeader
webdav.withHeader(c as Client, name as string, value as string)Send a header with every request, such as OCS-APIRequest: true.
c- the client
name- the header name
value- its value
- Returns
Client - the changed copy
- Throws
WebdavError - kind
"webdav"on a bad name, a value with a line break, or a header that frames the request (Host,Content-Length, ...)
withSkipVerify
webdav.withSkipVerify(c as Client)Accept any certificate. The connection stays encrypted but is no longer authenticated; prefer withCA.
c- the client
- Returns
Client - the changed copy
withTimeout
webdav.withTimeout(c as Client, ms as int)Set the idle timeout of each request.
c- the client
ms- milliseconds; 0 for the default, 30 s
- Returns
Client - the changed copy
withToken
webdav.withToken(c as Client, token as string)Authenticate with a bearer token.
c- the client
token- the token
- Returns
Client - the changed copy
- Throws
WebdavError - kind
"webdav"on a space or control character in it
Structs
Client
A connection to a WebDAV server below a base URL. Value-semantic: the with* functions return a changed copy.
def struct Client {
url as string,
basePath as string,
headers as map of string to string,
tls as http.TlsOptions,
timeoutMs as int,
allowInsecureHttp as bool
};url- the base URL without a trailing slash, e.g.
https://cloud.example.com/remote.php/dav/files/alice basePath- its path, percent-decoded; "" for the server root
headers- sent with every request (the credential among them)
tls- certificate checking
timeoutMs- the idle timeout of each request
allowInsecureHttp- send a credential over plain
http://to a non-loopback host (default false: such a request throws before it is sent)
PutOptions
How putWith writes a file.
def struct PutOptions {
contentType as string,
ifMatch as string,
onlyIfNew as bool
};contentType- the media type; "" for
application/octet-stream ifMatch- an entity tag the file must still have (from
Resource.etag), so a change made meanwhile is not overwritten; "" for none. It must be strong: a weakW/"..."tag can never match onlyIfNew- refuse to replace an existing file
Resource
A file or collection and its properties, as a PROPFIND reports it (and as a server answers one).
def struct Resource {
href as string,
path as string,
name as string,
isDir as bool,
size as int,
modified as int,
etag as string,
contentType as string,
quotaUsed as int,
quotaAvailable as int,
props as map of string to string
};href- the server path, percent-decoded, e.g.
/remote.php/dav/files/alice/a.txt; a collection's ends in/ path- the path below the client's URL, e.g.
/a.txt; "/" for the URL itself name- the last segment; "" for the server root
isDir- whether it is a collection
size- bytes (
getcontentlength); 0 for a collection modified- last change, Unix seconds (
getlastmodified); 0 when unknown etag- the entity tag as sent, quotes included, for
ifMatch contentType- the media type; "" when unknown
quotaUsed- bytes used (RFC 4331); -1 when not reported
quotaAvailable- bytes available (RFC 4331); -1 when not reported
props- every other property by its Clark name (
{http://owncloud.org/ns}fileid): its text, or its XML when it holds elements
WebdavError
A failed WebDAV operation. Every error of kind "webdav" is one, so its fields are safe to read once the kind matches.
def struct WebdavError {
kind as string,
message as string,
method as string,
path as string,
status as int,
member as string
};kind- always
"webdav"(ERROR_KIND) messagewebdav: <method> <path>:and what went wrongmethod- the request's method (
GET,PUT,MOVE, ...); "" for a local check path- the request's path; "" for a local check
status- the refused status: the response's (404, 409, 412 a failed
ifMatch, 423 locked, 507), or for a 207 Multi-Status the first failed member's; 0 when no response was read member- for a 207 Multi-Status, the path of the first member the server reports as failed; "" otherwise
Constants
webdav.DAV_NS as string- The
DAV:namespace of the WebDAV properties and elements. webdav.ERROR_KIND as string- The
kindof every error the module throws. webdav.MAX_WALK as int- The most entries
walkcollects before it refuses.