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 and 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 Sync Gateway in your Mac OS development environment. See the downloads site for all the available packages and see 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
Here's how to use 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 are available for download from this GitHub Repo.
git clone firstname.lastname@example.org:couchbaselabs/SyncGateway-Postman-Collection.git
There should be three files:
Sync-Gateway-Admin.postman_collection: This is the Portman collection corresponding to the Admin interface of the Sync Gateway.
Sync-Gateway-Public.postman_collection: This is the Portman collection corresponding to the Public interface of the Sync Gateway.
Sync-Gateway-Environment.postman_environment: This is the Environment Definitions file that defines the variables used by the Admin and Public collections.
Launch the Postman App
Launching the Postman app requires a couple of steps.
Import the Collections
Follow the steps in the video below to import the collections that were downloaded in the previous step.
Importing Sync Gateway postman collections.
Import the Environment File
Follow the steps in below to import the environment definition corresponding to the Postman collections:
Importing the environment definition for Admin and Public interfaces.
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 the Public port. It defaults to http://localhost:4985 and http://localhost:4984 respectively.
Setting the Postman environment.
That’s It! Run Your Queries
The following is a demonstration of running a request on the Admin interface:
Querying the Admin interface of Sync Gateway.
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.
Querying the public interface of Sync Gateway.
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.