Skip to content

Commit

Permalink
Added PATCH examples to docs. Thanks to @ryanisnan for the report.
Browse files Browse the repository at this point in the history
Added examples of using PATCH on the detail and list endpoints, and a note on PUT.
  • Loading branch information
bohde committed Feb 20, 2012
1 parent 6692eb2 commit af14a7b
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
41 changes: 41 additions & 0 deletions docs/interacting.rst
Expand Up @@ -526,6 +526,27 @@ We get a 204 status code, meaning our update was successful. We don't get
a ``Location`` header back because we did the ``PUT`` on a detail URL, which
presumably did not change.

.. note::

A ``PUT`` request requires that the entire resource representation be enclosed. Missing fields may cause errors, or be filled in by default values.


Partially Updating An Existing Resource (PATCH)
-----------------------------------------------

In some cases, you may not want to send the entire resource when updating. To update just a subset of the fields, we can send a ``PATCH`` request to the detail endpoint.::

curl --dump-header - -H "Content-Type: application/json" -X PATCH --data '{"body": "This actually is my last post."}' http://localhost:8000/api/v1/entry/4/


To which we should get back::

HTTP/1.0 202 ACCEPTED
Date: Fri, 20 May 2011 07:13:21 GMT
Server: WSGIServer/0.1 Python/2.7
Content-Length: 0
Content-Type: text/html; charset=utf-8


Updating A Whole Collection Of Resources (PUT)
----------------------------------------------
Expand Down Expand Up @@ -630,6 +651,26 @@ Gives us a 200 but no objects::
}


Bulk Operations
===============

As an optimization, it is possible to do many creations, updates, and deletions to a collection in a single request by sending a ``PATCH`` to the list endpoint.::

curl --dump-header - -H "Content-Type: application/json" -X PATCH --data '{"objects": [{"body": "Surprise! Another post!.", "pub_date": "2012-02-16T00:46:38", "slug": "yet-another-post", "title": "Yet Another Post"}], "deleted_objects": ["http://localhost:8000/api/v1/entry/4/"]}' http://localhost:8000/api/v1/entry/

We should get back::

HTTP/1.0 202 ACCEPTED
Date: Fri, 16 Feb 2012 00:46:38 GMT
Server: WSGIServer/0.1 Python/2.7
Content-Length: 0
Content-Type: text/html; charset=utf-8

The Accepted response means the server has accepted the request, but gives no details on the result. In order to see any created resources, we would need to do a get ``GET`` on the list endpoint.

For detailed information on the format of a bulk request, see :ref:`patch-list`.


You Did It!
===========

Expand Down
3 changes: 3 additions & 0 deletions docs/resources.rst
Expand Up @@ -1380,6 +1380,9 @@ Calls ``obj_delete``.
If the resource is deleted, return ``HttpNoContent`` (204 No Content).
If the resource did not exist, return ``HttpNotFound`` (404 Not Found).


.. _patch-list:

``patch_list``
--------------

Expand Down

0 comments on commit af14a7b

Please sign in to comment.