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:
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:
Similarly for deleting file say 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:
Creating new foder on Webdav Server:
To create new directory on webdav server use -X MKCOL request:
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 --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':
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:
It is always better to let curl decide the authentication method to be used and hence use --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.
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.
To use curl command for Two factor Authentication (2FA) on Webdav follow this post.