Over a million developers have joined DZone.

PowerShell With the Couchbase REST API

DZone's Guide to

PowerShell With the Couchbase REST API

If you’re a .NET/Windows developer, maybe you aren’t familiar with curl. In this post, I’ll show you how you can use PowerShell’s Invoke-WebRequest to make HTTP requests.

· Database Zone ·
Free Resource

Running out of memory? Learn how Redis Enterprise enables large dataset analysis with the highest throughput and lowest latency while reducing costs over 75%! 

PowerShell is a scripting environment/command line that comes with Windows and is also available for Linux and within Azure.

Maybe you’ve used Postman or Fiddler to make HTTP requests. Those are great, but not necessarily the right tools for automation or scripting.

You may have heard of curl before. It’s a command line tool for making HTTP requests.

If you’re a .NET/Windows developer (like me), maybe you aren’t familiar with curl. I use PowerShell as my default command line every day (though I still consider myself a PowerShell neophyte).

In this post, I’ll show you how you can use PowerShell’s Invoke-WebRequest to make HTTP requests (which you can use within PowerShell scripts for automation).

You can check out the PowerShell script I created on GitHub.

Note: As of the time of writing this post, I’m using PowerShell 5.1 on Windows 10.

Couchbase REST API

Couchbase Server has an extensive REST API that you can use to manage and administrate just about every aspect of Couchbase Server. For this blog post, I’m going to focus on the Full-Text Search (FTS) API. I’m going to show this because:

  • Creating an FTS index is something you’ll eventually want to automate.
  • You will probably want to share an FTS index you created with your team and/or check it into source control.
  • Couchbase Console already shows you exactly how to do it with curl.

I’m not going to cover FTS in detail. I invite you to check out past blog posts on FTS, and this short video demonstrating full-text search.

Full-Text Search Review

When you initially create an FTS index, you will probably use the built-in FTS UI in the Couchbase Console. This is fine when you are doing the initial development, but it’s not practical if you want to share this index with your team, automate deployment, or take advantage of source control.

Fortunately, you can use the Show index definition JSON feature to see the JSON data that make up the index definition. You can also have Couchbase Console generate the curl method for you.

Generate FTS curl script

Well, if you’re using curl, that’s very convenient. Here’s an example:


You can copy/paste that into a script, and check the script into source control. But what if you don’t use curl?

PowerShell Version: Invoke-WebRequest

First, create a new PowerShell script. I called mine createFtsIndex.ps1. All this PowerShell script is going to do is create an FTS index on an existing bucket.

You can start by pasting the curl command into this file. The bulk of this command is the JSON definition, which will be exactly the same.

Let’s break down the rest of the curl command to see what’s happening:

  • -XPUT: This is telling curl to use the PUT verb with the HTTP request.
  • -H "Content-Type: application/json": Use a Content-Type header.
  • http://localhost:8094/api/index/medical-condition: This is the URL of the REST endpoint. The localhost will vary based on where Couchbase is running, and the medical-condition part is just the name of the FTS index.
  • -d '…json payload…': The body of content that will be included in the HTTP request.

PowerShell’s Invoke-WebRequest can do all this stuff too, but the syntax is a bit different. Let’s step through the equivalents:

  • -Method PUT: This is telling Invoke-WebRequest to use the PUT verb with the HTTP request, so you can replace -XPUT.
  • -Header @{ … }: Specify headers to use with the request (more on this later).
  • -Uri http://localhost:8094/api/index/medical-condition": You just need to add -Uri in front.
  • -Body '…json payload…': The body of content is included this way instead of using curl’s -d


PowerShell expects a “dictionary” that contains headers. The syntax for a literal dictionary in PowerShell is @{"key1"="value1"; "key2"="value2"}.

So then, to specify Content-Type: -Headers @{"Content-Type"="application/json"}.

One thing that the curl output did not generate is the authentication information that you need to make a request to the API. With curl, you can specify basic authentication by adding the username/password to the URL. It will then translate it into the appropriate Basic Auth headers.

With PowerShell, it appears you have to do that yourself. My local Couchbase Server has credentials “Administrator” and “password” (please don’t use those in production). Those need to be encoded into Base64 and added to the headers.

Then the full Headers dictionary looks like this:

-Headers @{"Authorization" = "Basic "+[System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes("Administrator:password")); "Content-Type"="application/json"}

You might think that’s a bit noisy, and I agree. If you know a cleaner way to do this, I’m dying to know. Please leave a comment.

Execute the PowerShell Script

To execute the script, simply type .\createFtsIndex.ps1 at the PowerShell command line.

Execute PowerShell script

You’re now ready to make this a part of your deployment!

Running out of memory? Never run out of memory with Redis Enterprise databaseStart your free trial today.

database ,couchbase ,powershell ,rest api ,full-text search ,tutorial ,http requests

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}