WebDAV and POST

Over the past decade and a half, I’ve known that WebDAV exists (and been grateful for that fact), but I have mostly ignored it. Today, as an experiment, I set up an Apache WebDAV server on my laptop. It’s great, except that it’s missing one thing I’d always assumed it would have: the ability to upload a file and let the server decide the URL.

This is an important feature for any web repository that’s shared among multiple users or client systems. For example, let’s say that User A wants to upload a file named funny-cat-pic.jpg and User B also wants to upload a file named funny-cat-pic.jpg.

Using HTTP PUT, they can both specify that it belongs in /pix/funny-cat-pic.jpg, but then the second one will simply overwrite the first one.

Using HTTP POST, on the other hand, they could each upload their files, and then receive HTTP 303 (See Other) 201 (Created) responses providing the URLs that the WebDAV server chose, e.g. /pix/funny-cat-pic-001.jpg and /pix/funny-cat-pic-002.jpg. The server could guarantee that a POSTed file never overwrote an existing one.

I know I can just write a short Python or PHP script to do the posting, but before I do that, am I missing anything? Supporting POST seems like it should have been an obvious choice from the start.

Update: Thanks to Tim Bray for the HTTP status correction. Tim also mentioned that AtomPub supports POST, but not, unfortunately, hierarchical organisation of resources (e.g. directories).

This entry was posted in General. Bookmark the permalink.

3 Responses to WebDAV and POST

  1. John Cowan says:

    The reason WebDAV doesn’t provide what you want is that it is a file-server protocol like FTP, which assumes that the client is in control of the names used on the file space. When you write a file to some network drive, you are expected to specify the name it will have on that drive. If someone else has already written a file with that name, tant pis — and if someone else comes along and writes a file with that same name after you, you lose.

    In general, the model of POST, when properly used in a REST style, is that the server makes up the URL of the new entry and tells the client about it. There is nothing to prevent the client from providing a hint, but I don’t think it makes sense for the hint to be in the URL itself. Rather the URL should point to a container where the new object will reside, and you’ll get back an extended URL specifying the new object’s server-created name appended to the container URL.

  2. That’s a good, clear distinction, John, and it will help me decide which use cases do or don’t lead to WebDAV as a solution.

  3. Hello says:

    If you’re expecting to upload a new file, you do a conditional PUT. The pre-condition will fail if some one else beets you to it.

Comments are closed.