©2015 - 2022 Chad’s Technoworks. Disclaimer and Terms of Use

Chad’s TechnoWorks My Journal On Technology

Information Technology

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 Database Status

Sync Gateway Bulk Load Using CURL

Inserting A Document In Sync Gateway Using CURL

Sync Gateway Document Query Using CURL

  Retrieve All Documents

  Single Document Query

Updating Documents In Sync Gateway Using CURL

  Single Document Update

Deleting Documents In Sync Gateway Using CURL



SYNC GATEWAY DATABASE STATUS


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 -X POST http://host:4984/{db}/_bulk_docs \

  -H "Content-Type: application/json" \

  -d '{"docs": [{"doc1key1":"value1","doc1key2":"value2",..},{"doc2key1":"value1",..}]}'


           

curl -X POST http://lxnode15.vlabs.net:4985/syncdb/_bulk_docs \

  -H "Content-Type: application/json" \

  -d '{"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"}]}'           



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


RETRIEVE ALL DOCUMENTS


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 -X GET "http://lxnode15.vlabs.net:4984/syncdb/_all_docs"

{"rows":[

{"key":"7b83809f47c9b0e821c61241baa3fb84","id":"7b83809f47c9b0e821c61241baa3fb84","value":{"rev":"1-6ac5846ca61b09501c442f496cd966af"}}

,{"key":"fddfe7354b2defd907aff1ccc0421110","id":"fddfe7354b2defd907aff1ccc0421110","value":{"rev":"1-df763224a88d8983f63b5ec41e9d541c"}}

,{"key":"ford::Fiesta::2012","id":"ford::Fiesta::2012","value":{"rev":"1-1465a7a82cae33ce68691cdc70ea4d43"}}

,{"key":"honda::odyssey::2012","id":"honda::odyssey::2012","value":{"rev":"1-2cd37f753cdd017e51478f632547cf7e"}}

,{"key":"toyota::corolla::2010","id":"toyota::corolla::2010","value":{"rev":"1-875e75fe2e9837c495656fa1728105a5"}}

],

"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 -X GET "http://lxnode15.vlabs.net:4984/syncdb/_all_docs?limit=3&include_docs=true"

{"rows":[

{"key":"7b83809f47c9b0e821c61241baa3fb84","id":"7b83809f47c9b0e821c61241baa3fb84","value":{"rev":"1-6ac5846ca61b09501c442f496cd966af"},"doc":{"Address":"Chicago IL","DealerID":"IL001","DocType":"Dealer","Name":"Bonanza Cars","_id":"7b83809f47c9b0e821c61241baa3fb84","_rev":"1-6ac5846ca61b09501c442f496cd966af"}}

,{"key":"fddfe7354b2defd907aff1ccc0421110","id":"fddfe7354b2defd907aff1ccc0421110","value":{"rev":"1-df763224a88d8983f63b5ec41e9d541c"},"doc":{"Address":"Woodstock IL","DealerID":"IL002","DocType":"Dealer","Name":"Ford of Woodstock","_id":"fddfe7354b2defd907aff1ccc0421110","_rev":"1-df763224a88d8983f63b5ec41e9d541c"}}

,{"key":"ford::Fiesta::2012","id":"ford::Fiesta::2012","value":{"rev":"2-eb5efa9b1cc34dc5aaf563cc090466d3"},"doc":{"_id":"ford::Fiesta::2012","_rev":"2-eb5efa9b1cc34dc5aaf563cc090466d3","docs":[{"DocType":"UsedCar","Engine":"1.8 Liter 4 cyl","Manufacturer":"Ford","Model":"Fiesta","Model_Year":"2012","VehicleType":"Sedan"}]}}

],

"total_rows":3,"update_seq":7}

[chad@lxnode15 ~]$



SINGLE DOCUMENT QUERY


Get /{db}/{docID}


[chad@lxnode15 ~]$ curl -X GET "http://lxnode15.vlabs.net:4984/syncdb/toyota::corolla::2010"

{"DocType":"UsedCar","Engine":"2.2 Liter 4 cyl","Manufacturer":"Toyota","Model":"Corolla","Model_Year":"2010","VehicleType":"Sedan","_id":"toyota::corolla::2010","_rev":"1-875e75fe2e9837c495656fa1728105a5"}

[chad@lxnode15 ~]$


INSERTING A DOCUMENT IN SYNC GATEWAY USING CURL


Syntax:


curl -X PUT 'http://host:4984/{db}/{docID}' -H "Content-Type: application/json" \

  -d '{"docs": [{"key1":"value1","key2":"value2",..}]}'



curl -X PUT 'http://lxnode15.vlabs.net:4984/syncdb/nissan::sentra::2014' -H "Content-Type: application/json" \

   -d '{"docs": [{"DocType":"UsedCar","Engine":"1.6 Liter 4 cyl","Manufacturer":"Nissan","Model":"Sentra","Model_Year":"2014","VehicleType":"Sedan"}]}'


[chad@lxnode15 ~]$ curl -X PUT 'http://lxnode15.vlabs.net:4984/syncdb/nissan::sentra::2014' -H "Content-Type: application/json" \

>    -d '{"docs": [{"DocType":"UsedCar","Engine":"1.6 Liter 4 cyl","Manufacturer":"Nissan","Model":"Sentra","Model_Year":"2014","VehicleType":"Sedan"}]}'

{"id":"nissan::sentra::2014","ok":true,"rev":"1-e5c100eaa50ce9e91dd0fa9eb9366b6a"}

[chad@lxnode15 ~]$


UPDATING DOCUMENTS IN SYNC GATEWAY USING CURL


SINGLE DOCUMENT UPDATE


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 -X GET "http://lxnode15.vlabs.net:4984/syncdb/ford::Fiesta::2012"

{"_id":"ford::Fiesta::2012","_rev":"1-1465a7a82cae33ce68691cdc70ea4d43","docs":[{"DocType":"UsedCar","Engine":"1.8 Liter 4 cyl","Manufacturer":"Ford","Model":"Fiesta","Model_Year":"2012","VehicleType":"Sedan"}]}

[chad@lxnode15 ~]$


Then proceed to update the document with the revision sequence number (_rev value).


Syntax:

curl -X PUT 'http://host:4984/{db}/{docID}?rev={_rev}' -H "Content-Type: application/json" \

  -d '{"docs": [{"key1":"value1","key2":"value2",..}]}'


[chad@lxnode15 ~]$ curl -X PUT 'http://lxnode15.vlabs.net:4984/syncdb/ford::Fiesta::2012?rev=1-1465a7a82cae33ce68691cdc70ea4d43' -H "Content-Type: application/json" \

>   -d '{"docs": [{"DocType":"UsedCar","VehicleType": "Sedan","Manufacturer":"Ford","Model":"Fiesta","Model_Year":"2012","Engine":"1.8 Liter 4 cyl"}]}'

{"id":"ford::Fiesta::2012","ok":true,"rev":"2-eb5efa9b1cc34dc5aaf563cc090466d3"}

[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 -X DELETE "http://SyncGatewayHost:4984/{db}/{docID}?rev=string" -H "Content-Type: application/json"


Sample:


Retrieve current revision:

[chad@lxnode15 ~]$ curl -X GET "http://lxnode15.vlabs.net:4984/syncdb/toyota::corolla::2010"

{"DocType":"UsedCar","Engine":"2.2 Liter 4 cyl","Manufacturer":"Toyota","Model":"Corolla","Model_Year":"2010","VehicleType":"Sedan","_id":"toyota::corolla::2010","_rev":"1-875e75fe2e9837c495656fa1728105a5"}

[chad@lxnode15 ~]$


Proceed with delete:

curl -X DELETE "http://lxnode15.vlabs.net:4984/syncdb/toyota::corolla::2010?rev=1-875e75fe2e9837c495656fa1728105a5" -H "Content-Type: application/json"


[chad@lxnode15 ~]$ curl -X DELETE "http://lxnode15.vlabs.net:4984/syncdb/toyota::corolla::2010?rev=1-875e75fe2e9837c495656fa1728105a5"

{"id":"toyota::corolla::2010","ok":true,"rev":"2-b4f689caff22fed0baaf30792b5f7f47"}

[chad@lxnode15 ~]$