Upgrade Your Drupal Skills

We trained 1,000+ Drupal Developers over the last decade.

See Advanced Courses NAH, I know Enough

Using Curl commands with Webdav

Parent Feed: 

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: http://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 'http://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 'http://example.com/webdav/test'

Similarly for deleting file say test.txt:

curl -X DELETE 'http://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' 'http://example.com/old.txt'
  • Creating new foder on Webdav Server:

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

curl -X MKCOL 'http://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' 'http://example.com/test/'

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' 'http://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' 'http://example.com' --basic

curl --user 'user:pass' 'http://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' 'http://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 'http://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.

Original Post: 

About Drupal Sun

Drupal Sun is an Evolving Web project. It allows you to:

  • Do full-text search on all the articles in Drupal Planet (thanks to Apache Solr)
  • Facet based on tags, author, or feed
  • Flip through articles quickly (with j/k or arrow keys) to find what you're interested in
  • View the entire article text inline, or in the context of the site where it was created

See the blog post at Evolving Web

Evolving Web