PUT /{db}/{doc}

In this document

This request creates a new document or creates a new revision of an existing document. It enables you to specify the identifier for a new document rather than letting the software create an identifier. If you want to create a new document and let the software create an identifier, use the POST /db request.

If the document specified by doc does not exist, a new document is created and assigned the identifier specified in doc. If the document already exists, the document is updated with the JSON document in the message body and given a new revision.

Request


Request headers

Query parameters

NameTypeDescriptionDefault
batch stringStores the document in batch mode. To use, set the value to ok.none
rev stringRevision identifiernone

Response


Status codes

Response headers

Message body

The response is a JSON document that contains the following objects:

NameTypeDescription
id StringDocument identifier
ok BooleanIndicates whether the operation was successful
rev Stringrevision identifier

Example


The following example creates a new document with the identifier ButterChicken in a database named cookbook.

Request

PUT /cookbook/ButterChicken HTTP/1.1
Host: localhost:59840
Content-Type: application/json
{
    "description": "A very buttery chicken dish",
    "nutrition": "500 calories",
    "servings": 4,
    "title": "Butter Chicken"
}

Response

HTTP/1.1 201 Created
Accept-Ranges: bytes
Content-Length: 92
Content-Type: application/json
Date: Fri, 13 Dec 2013 19:34:04 GMT
Etag: "1-4101356e9c47d15d4f8f7390d05dbbcf"
Location: http://localhost:59840/cookbook/ButterChicken
Server: CouchbaseLite 1.486
{
  "id" : "ButterChicken",
  "rev" : "1-4101356e9c47d15d4f8f7390d05dbbcf",
  "ok" : true
}

The following example updates the document with the identifier ButterChicken that was created in the previous example. To update an existing document, the revision identifier of the current document must be supplied in an If-Match header or a rev query parameter. This example shows how to use both methods of specifying the revision.

Request

This form of the request uses the If-Match header to supply the revision identifier:

PUT /cookbook/ButterChicken HTTP/1.1
Host: localhost:59840
Content-Type: application/json
If-Match: 1-4101356e9c47d15d4f8f7390d05dbbcf
{
    "description": "A very buttery chicken dish",
    "nutrition": "500 calories",
    "serving-suggestion": "Serve with rice and green beans.",
    "servings": 4,
    "title": "Butter Chicken"
}

To use the rev query parameter rather than the If-Match header to supply the revision identifier, structure the request like this:

PUT /cookbook/ButterChicken?rev=1-4101356e9c47d15d4f8f7390d05dbbcf HTTP/1.1
Host: localhost:59840
Content-Type: application/json
{
    "description": "A very buttery chicken dish",
    "nutrition": "500 calories",
    "serving-suggestion": "Serve with rice and green beans.",
    "servings": 4,
    "title": "Butter Chicken"
}

Response

HTTP/1.1 201 Created
Accept-Ranges: bytes
Content-Length: 92
Content-Type: application/json
Date: Fri, 13 Dec 2013 19:56:09 GMT
Etag: "2-1b76d07281d4a576130c7d8f9f621d5e"
Location: http://localhost:59840/cookbook/ButterChicken
Server: CouchbaseLite 1.486
{
  "id" : "ButterChicken",
  "rev" : "2-1b76d07281d4a576130c7d8f9f621d5e",
  "ok" : true
}