This post discusses a convenient way to query, explore and test the REST API exposed by the Couchbase Mobile Sync Gateway using the Postman Chrome Developer tool. Sync Gateway exposes a REST, Batch & Stream interface that allows clients interact with it over the Internet.
NOTE: We will be discussing Couchbase Mobile v1.4 which is the current production release. There is a newer Developer Preview version 2.0 of Couchbase Mobile
Couchbase Sync Gateway is part of the Couchbase Mobile stack and is an Internet-facing synchronization mechanism that securely syncs data across devices as well as between devices and the cloud. There are two ports over which the Sync Gateway listens to requests- The Admin port (defaults to 4985) and the Public port (defaults to 4984). In production deployments, the admin port is typically blocked from access over the Internet.
Installation of Couchbase Sync Gateway
Please follow instructions in the blog post to install the the Sync Gateway in your Mac OS development environment. See the downloads site for all the available packages, and the full installation guide for complete details. To install on Linux distributions other than the supported ones, see this post.
Installation of Postman
Using Postman to Query the Couchbase Sync Gateway
Get the Postman collections
The Postman collection files and environment definition for the Admin and Public interface of the Sync Gateway is available for download from this Github Repo.
git clone https://github.com/couchbaselabs/Couchbase-Sync-Gateway-Postman-Collection.git
There should be three files:-
This is the Portman collection corresponding to the Admin interface of the Sync Gateway
This is the Portman collection corresponding to the Public interface of the Sync Gateway
This is the Environment Definitions file that defines the variables used by the Admin and Public collections
Launch the Postman App
Import the collections
Follow the steps in the video below to import the collections that were downloaded in the previous step.
Import the environment file
Follow the steps in the video below to import the environment definition corresponding to the Postman collections
Set the appropriate environment
Follow the steps in the video below to set the environment to the one that you just imported. Update the values of the variables to suit your environment. Make sure that the adminurl points to the Sync Gateway at Admin Port and the publicurl variable points to the Sync Gateway at Public port. It defaults to http://localhost:4985 and http://localhost:4984 respectively.
That’s it! Run your queries
The following is a demonstration of running a request on the Admin Interface
The following is a demonstration of running a request on the Public Interface. Make sure you set the authorization header is set appropriately to use the appropriate authentication mechanism.
If you are interested in executing the Postman Collection from the command line interface, which may be the case for instance, if you want to integrate this as part of your Continuous Integration process, then you should check out Newman, which is CLI runner for Postman.
For example, the command below will run the Sync-Gateway-Admin.postman_collection with the Sync-Gateway-Environment file.
If your Sync Gateway is running with SSL enabled and is using a self-signed certificate, the “-k” option will suppress validation of the cert (“insecure” SSL connection). This is not recommended in production environments.
newman run -k Sync-Gateway-Admin.postman_collection --environment Sync-Gateway-Environment.postman_environment --bail --delay-request 300;
The Postman Collections provide an easy way to query, explore and test the REST interface exposed by the Sync Gateway. If you see an issue in the collections definitions or if you would like to enhance it, please submit a Pull Request to the Github Repo.
The Couchbase Mobile Dev Forums is another great place to get your mobile related questions answered .
Also, checkout out the Couchbase Sync Gateway API Definitions for details on the web interface.