©2015 -
Sample Sync Gateway Data Update Using CURL
The current Couchbase Public REST API reference guide only provides parameter descriptions but lacks examples of http calls.
These are my notes that serves as my template for REST API calls using CURL for Couchbase Sync Gateway.
TABLE OF CONTENTS
Sync Gateway Bulk Load Using CURL
Inserting A Document In Sync Gateway Using CURL
Sync Gateway Document Query Using CURL
Updating Documents In Sync Gateway Using CURL
Deleting Documents In Sync Gateway Using CURL
GET /{db}/
[chad@lxnode15 ~]$ curl http://lxnode15.vlabs.net:4984/syncdb/
{"committed_update_seq":1,"compact_running":false,"db_name":"syncdb","disk_format_version":0,"instance_start_time":1531265732497424,"purge_seq":0,"state":"Online","update_seq":1}
[chad@lxnode15 ~]$
SYNC GATEWAY BULK LOAD USING CURL
sample documents:
{"docs": [
{"_id": "toyota::corolla::2010",
"DocType":"UsedCar",
"VehicleType": "Sedan",
"Manufacturer":"Toyota",
"Model":"Corolla",
"Model_Year":"2010",
"Engine":"2.2 Liter 4 cyl"},
{"_id": "ford::Fiesta::2012",
"DocType":"UsedCar",
"VehicleType": "Sedan",
"Manufacturer":"Ford",
"Model":"Fiesta",
"Model_Year":"2012",
"Engine":"2.0 Liter 4 cyl"},
{"_id": "honda::odyssey::2012",
"DocType":"CourtesyCar",
"VehicleType": "Minivan",
"Manufacturer":"Honda",
"Model":"Odyssey",
"Model_Year":"2016",
"Engine":"2.4 Liter 4 cyl"},
{"DocType":"Dealer","DealerID":"IL001","Name":"Bonanza Cars","Address":"Chicago IL"},
{"DocType":"Dealer","DealerID":"IL002","Name":"Ford of Woodstock","Address":"Woodstock IL"}
]
}
Syntax:
curl -
-
-
curl -
-
-
NOTE: The Example documents loaded into the sync gateway have 3 documents with specific document ID (_id) for the car documents, while the dealer data had a system generated doc id.
SYNC GATEWAY DOCUMENT QUERY USING CURL
GET /{db}/_all_docs
Optional parameters:
access (boolean)
Indicates whether to include in the response a list of what access this document grants (i.e. which users it allows to access which channels.) This option may only be used from the admin port.
channels (boolean)
Indicates whether to include in the response a channels property containing an array of channels this document is assigned to. (Channels not accessible by the user making the request will not be listed.)
include_docs (boolean)
Default is false. Indicates whether to include the associated document with each result. If there are conflicts, only the winning revision is returned.
revs (boolean)
Default is false. Indicates whether to include a _revisions property for each document in the response, which contains a revision history of the document. The length of the returned revision tree can be specified with the revs_limit querystring parameter.
limit (integer)
Limits the number of result rows to the specified value. Using a value of 0 has the same effect as the value 1.
[chad@lxnode15 ~]$ curl -
{"rows":[
{"key":"7b83809f47c9b0e821c61241baa3fb84","id":"7b83809f47c9b0e821c61241baa3fb84","value":{"rev":"1-
,{"key":"fddfe7354b2defd907aff1ccc0421110","id":"fddfe7354b2defd907aff1ccc0421110","value":{"rev":"1-
,{"key":"ford::Fiesta::2012","id":"ford::Fiesta::2012","value":{"rev":"1-
,{"key":"honda::odyssey::2012","id":"honda::odyssey::2012","value":{"rev":"1-
,{"key":"toyota::corolla::2010","id":"toyota::corolla::2010","value":{"rev":"1-
],
"total_rows":5,"update_seq":6}
[chad@lxnode15 ~]$
Notice that the above result set is purely meta info and didn't include the document contents.
The following query would help provide the contents utilizing the include_docs parameter:
[chad@lxnode15 ~]$ curl -
{"rows":[
{"key":"7b83809f47c9b0e821c61241baa3fb84","id":"7b83809f47c9b0e821c61241baa3fb84","value":{"rev":"1-
,{"key":"fddfe7354b2defd907aff1ccc0421110","id":"fddfe7354b2defd907aff1ccc0421110","value":{"rev":"1-
,{"key":"ford::Fiesta::2012","id":"ford::Fiesta::2012","value":{"rev":"2-
],
"total_rows":3,"update_seq":7}
[chad@lxnode15 ~]$
Get /{db}/{docID}
[chad@lxnode15 ~]$ curl -
{"DocType":"UsedCar","Engine":"2.2 Liter 4 cyl","Manufacturer":"Toyota","Model":"Corolla","Model_Year":"2010","VehicleType":"Sedan","_id":"toyota::corolla::2010","_rev":"1-
[chad@lxnode15 ~]$
INSERTING A DOCUMENT IN SYNC GATEWAY USING CURL
Syntax:
curl -
-
curl -
-
[chad@lxnode15 ~]$ curl -
> -
{"id":"nissan::sentra::2014","ok":true,"rev":"1-
[chad@lxnode15 ~]$
UPDATING DOCUMENTS IN SYNC GATEWAY USING CURL
Updating a document requires you to pass the Doc ID and the Rev Sequence Number.
First you need to acquire the current revision sequence number (_rev) of your target document.
[chad@lxnode15 ~]$ curl -
{"_id":"ford::Fiesta::2012","_rev":"1-
[chad@lxnode15 ~]$
Then proceed to update the document with the revision sequence number (_rev value).
Syntax:
curl -
-
[chad@lxnode15 ~]$ curl -
> -
{"id":"ford::Fiesta::2012","ok":true,"rev":"2-
[chad@lxnode15 ~]$
NOTE: The result set provides you a new revision sequence. When another user has held an older revision number and applies an update, an error will occur and he is required to fetch a new revision number and data contents to examine and apply his changes.
DELETING DOCUMENTS IN SYNC GATEWAY USING CURL
To delete a document, it requires also the current revision sequence id.
Syntax:
curl -
Sample:
Retrieve current revision:
[chad@lxnode15 ~]$ curl -
{"DocType":"UsedCar","Engine":"2.2 Liter 4 cyl","Manufacturer":"Toyota","Model":"Corolla","Model_Year":"2010","VehicleType":"Sedan","_id":"toyota::corolla::2010","_rev":"1-
[chad@lxnode15 ~]$
Proceed with delete:
curl -
[chad@lxnode15 ~]$ curl -
{"id":"toyota::corolla::2010","ok":true,"rev":"2-
[chad@lxnode15 ~]$