Skip to main content

Overwriting a file

Replacing the contents of an existing file very similar as Uploading a new file, by sending a PUT request with the new contents of the file. This PUT must go to the IRI for the media object representing the file - that is, the URL indicated by the link rel="edit-media" element in the Atom representation.

We can get this URL by querying the Atom representation of a resource:

mat@augustus:~$ curl -i -X GET -u cuscav 
Enter host password for user 'cuscav':

HTTP/1.1 200 OK
Date: Tue, 05 Mar 2013 18:25:58 GMT
Content-Type: application/atom+xml;charset=utf-8

<?xml version="1.0" encoding="UTF-8"?>
<entry xmlns="" xmlns:app=""> xmlns:files=""
  <title>Exam Results.pdf</title>
  <link rel="edit" href=""/>
  <link rel="edit-media" href=""/>
  <link rel="alternate" type="application/pdf" href=""/>
  <content type="application/pdf" src=""/>
    <name>Mathew Mannion</name>
  <summary type="text">Exam Results.pdf</summary>

This is always identical to the Atom representation of a resource, but with /media instead of /node in the URL.

We can now overwrite the file with a PUT to this URL:

mat@augustus:~$ curl -i -X PUT --data-binary @newcontent.pdf -H 'Content-Type: application/pdf' -H 'Slug: Exam Results.pdf' -u cuscav 
Enter host password for user 'cuscav':

HTTP/1.1 200 OK
Date: Tue, 05 Mar 2013 18:34:41 GMT
Content-Length: 0

The server responds with a simple 200 OK to indicate the content has been overwritten.

Note that sending anything other than a PUT request to the media endpoint will result in a 405 Method Not Allowed - actions such as renaming folders and files must use the Atom representation.