Skip to main content Skip to navigation

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 
   "https://files.warwick.ac.uk/files/api/atom/node?account=mmannion&path=mmannion/Files/Exam_Results.pdf"
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="http://www.w3.org/2005/Atom" xmlns:app="http://www.w3.org/2007/app"> xmlns:files="http://go.warwick.ac.uk/elab-schemas/files/atom"
  <title>Exam Results.pdf</title>
  <link rel="edit" href="https://files.warwick.ac.uk/files/api/atom/node?account=mmannion&path=mmannion%2FFiles%2FExam+Results.pdf"/>
  <link rel="edit-media" href="https://files.warwick.ac.uk/files/api/atom/media?account=mmannion&path=mmannion%2FFiles%2FExam+Results.pdf"/>
  <link rel="alternate" type="application/pdf" href="https://files.warwick.ac.uk/mmannion/files/Exam+Results.pdf"/>
  <content type="application/pdf" src="https://files.warwick.ac.uk/mmannion/files/Exam+Results.pdf"/>
  <id>https://files.warwick.ac.uk/mmannion/files/Exam+Results.pdf</id>
  <updated>2013-03-05T16:38:34.971Z</updated>
  <published>2013-03-05T16:38:35.023Z</published>
  <app:edited>2013-03-05T16:38:34.971Z</app:edited>
  <author>
    <name>Mathew Mannion</name>
  </author>
  <summary type="text">Exam Results.pdf</summary>
  <files:mimetype>application/pdf</files:mimetype>
  <files:size>11923854</files:size>
  <files:antivirus>
    <files:checking>false</files:checking>
    <files:checked>true</files:checked>
    <files:hasvirus>false</files:hasvirus>
  </files:antivirus>
</entry>  

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 
   "https://files.warwick.ac.uk/files/api/atom/media?account=mmannion&path=mmannion/Files/Exam+Results.pdf"
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.