Query multiple documents in one Rest API call


#1

I hope this is a simple question with a quick answer. I would just like to perform one REST API HTTP call and retrieve multiple documents at once. I know all the document ID’s that I need.

For example, I need to get 100 documents. I have an array of the 100 document ID’s. I would prefer not to have to do a REST API call for getting each document individually.

What would be the Sync Gateway REST API url for doing this? Does one exist?

Thank you,
Sean


#2

Bulk Get sounds like exactly what you’re looking for :slight_smile:


#3

That is exactly what I was looking for! Thank you.


#4

Okay… The command may be exactly what I need… But when executed, I run into a HTML error code 405 method not supported.

I am running node.js to communicate with the couchbase server via sync gateway REST API. I can do a GET request for a single document just fine

var request = new XMLHttpRequest();
var url = dbUrl + doc_id + “?attachments=false&revs=false&show_exp=false”;
request.open(‘GET’, url, false); // false makes the request synchronous
request.setRequestHeader(“Content-type”, “application/json”);
request.send(null);

The URL ends up looking like this and works great!
https://my_host:4985/my_db/my_doc_id?attachments=false&revs=false&show_exp=false

But when I do the _bulk_get

let params = {“docs”:doc_id_arr}; // id array is formated correctly here to produce correct json
var json_params = JSON.stringify(params);

var url = dbUrl + “_bulk_get?revs=false&attachments=false”;
request.open(‘GET’, url, false); // false makes the request synchronous
request.setRequestHeader(“Content-type”, “application/json”);
request.send(json_params);

The above fails with html status code 405. Any idea why? Do I need to enable something in the sync_gateway config file?


#5

Answering my own question regarding HTML error code 405. I did two things.

  1. I added the “Accept” header to my CORS in sync_gateway_confg file. Not sure if it was this that actual solved it though.
    “CORS”:
    {
    “Origin”: [“http://localhost:8080”],
    “LoginOrigin”: [“http://localhost:8080”],
    “Headers”: [“Content-Type”, “Accept”, “authorization”],
    “MaxAge”: 17280000
    },

Then I added the “Accept” header in my http request. Plus changed it to a POST.

var request = new XMLHttpRequest();
request.open(‘POST’, url, false); // false makes the request synchronous
request.setRequestHeader(“Content-Type”, “application/json”);
request.setRequestHeader(“Accept”, “application/json”);
request.send(JSON.stringify(params));


#6

The CORS configuration and Accept header should not have any affect on the “Method Not Found”. It’s a POST call (as specified in the documentation) - so switching from GET to POST should do it.