Drupal
  |  16th December, 2015

Using Curl commands with Webdav

PURUSHOTAM RAI

Curl is a command line tool for doing all sorts of URL manipulations and transfers, but this particular post will focus on how to use curl for managing (read/ delete/ rename/ upload) files on Webdav Server. While the second part of the post will cover implementation of Two Factor Authentication for the same. I'll assume that you know how to execute 'curl' command using terminal and invoke 'curl --help' or 'curl --manual' to get basic information about it.

Assume we have following Data:

Webdav URL: https://example.com/webdav
Username: user
Password: pass

Note: It is recommended to read article completely (both management and curl options) before implementation.

  • Reading Files/Folders on Webdav Server:

Reading files or folders on Webdav Server is simply implementation of Curl Get request. So, the command would be:

curl 'https://example.com/webdav'

Also Optionally, you can get response headers from the webdav server, based on which you can get to know about completion of the operation. Click here

  • Deleting Files/Folders on Webdav Server:

For deleting an existing file or folder on Webdav Server we need to send DELETE request followed by path to the folder/file of the webdav server. Let's say we need to delete test folder on webdav server, so our command would be:

curl -X DELETE 'https://example.com/webdav/test'

Similarly for deleting file say test.txt:

curl -X DELETE 'https://example.com/webdav/test.txt'

On few Webdav Servers, it is possible that curl -X DELETE command is not able to delete folder because of index.* file in that folder. In that case try renaming index file first.

  • Renaming File on Webdav Server:

Rename operation can be performed using -X MOVE request. Let's say we wanna rename old.txt to new.txt:

curl -X MOVE --header 'Destination:http://example.org/new.txt' 'https://example.com/old.txt'
  • Creating new foder on Webdav Server:

To create new directory on webdav server use -X MKCOL request:

curl -X MKCOL 'https://example.com/new_folder'

 

  • Uploading File on Webdav Server:

For uploading file on webdav server use PUT request. Let's say src file is /path/to/local/file.txt and we want to upload it in test folder on webdav server, so our command would be:

curl -T '/path/to/local/file.txt' 'https://example.com/test/'

CURL --Options:

Curl provides various options which makes our life easier. In this article we will focus on few necessary options required for basic webdav access.

  • Username/Password

To specify username and password for webdav, use '--user' or use alias '-u':

curl --user 'user:pass' 'https://example.com'
  • HTTP Authentication

Authentication is the ability to tell the server your username and password so that it can verify that you're allowed to do the request you're doing. Commonly used Authentications in case of webdav are : Basic and Digest.

Usage:

curl --user 'user:pass' 'https://example.com' --basic

curl --user 'user:pass' 'https://example.com' --digest

It is always better to let curl decide the authentication method to be used and hence use --anyauth:

curl --user 'user:pass' 'https://example.com' --anyauth
  • Get Response Code

We definitely need a way to know if our request performed desired operation successfully on Remote Webdav Server or not. In that case, there are various options provided by curl which could be useful. One of them is to get response code and hence analyze the behaviour.

curl --user 'user:pass' -X DELETE 'https://example.com/test' -sw '%{http_code}'

This command will output only the response code sent in response header by Webdav Server. Let's say the output is: 200, this means operation completed successfully. So, different http codes correspond to different meaning. For detailed information follow this link.

To use curl command for Two factor Authentication (2FA) on Webdav follow this post.

Looking for a Drupal partner ?

We are drupal 8 ready