jsonibm-cloudcouchdbcloudantcouchdb-nano

Json error while trying to use bulk document create on CouchDB(Cloudant) database


Per this https://docs.cloudant.com/document.html#bulk-operations I am trying to insert multiple documents into my cloudant database but I get an error:

{
  "error": {
    "statusCode": 400,
    "name": "Error",
    "request": {
      "method": "POST",
      "headers": {
        "content-type": "application/json",
        "accept": "application/json"
      },
      "uri": "https://XXXXXX:XXXXXX@8f7fc7f0-766b-4429-b060-4ef1c01f7665-bluemix.cloudant.com/ttt/_bulk_docs",
      "body": "[{\"name\":\"Nicholas\",\"_id\":\"96f898f0-f6ff-4a9b-aac4-503992f31b01\",\"_attachments\":{},\"age\":45,\"gender\":\"male\"},{\"name\":\"Taylor\",\"_id\":\"5a049246-179f-42ad-87ac-8f080426c17c\",\"_attachments\":{},\"age\":50,\"gender\":\"male\"},{\"name\":\"Owen\",\"_id\":\"d1f61e66-7708-4da6-aa05-7cbc33b44b7e\",\"_attachments\":{},\"age\":51,\"gender\":\"male\"}]"
    },
    "description": "couch returned 400",
    "scope": "couch",
    "reason": "Request body must be a JSON object",
    "error": "bad_request",
    "stack": "Error: Request body must be a JSON object\n    at Request._callback (/nodejsAction/node_modules/cloudant-nano/lib/nano.js:247:15)\n    at Request.self.callback (/nodejsAction/node_modules/request/request.js:186:22)\n    at emitTwo (events.js:106:13)\n    at Request.emit (events.js:191:7)\n    at Request.<anonymous> (/nodejsAction/node_modules/request/request.js:1081:10)\n    at emitOne (events.js:96:13)\n    at Request.emit (events.js:188:7)\n    at IncomingMessage.<anonymous> (/nodejsAction/node_modules/request/request.js:1001:12)\n    at IncomingMessage.g (events.js:291:16)",
    "message": "Request body must be a JSON object",
    "errid": "non_200",
    "headers": {
      "statusCode": 400,
      "x-couchdb-body-time": "0",
      "cache-control": "must-revalidate",
      "uri": "https://XXXXXX:XXXXXX@8f7fc7f0-766b-4429-b060-4ef1c01f7665-bluemix.cloudant.com/ttt/_bulk_docs",
      "x-cloudant-backend": "bm-cc-dal-01",
      "date": "Sat, 20 May 2017 12:11:37 GMT",
      "content-type": "application/json",
      "via": "1.1 lb1.bm-cc-dal-01 (Glum/1.34.0)",
      "x-couch-request-id": "fed1512052",
      "x-content-type-options": "nosniff",
      "strict-transport-security": "max-age=31536000"
    }
  }
}

I took the input Json from the example docs and checked it online JSON validator it seems to be ok. What I am doing wrong?


Solution

  • It appears that the request body is missing the docs property. The referenced documentation provides this example:

    {
      "docs": [
               {
                "name": "Nicholas",
                "age": 45,
                "gender": "female",
                "_id": "96f898f0-f6ff-4a9b-aac4-503992f31b01",
                "_rev": "1-54dd23d6a630d0d75c2c5d4ef894454e"
               },
               {
                "_id": "d1f61e66-7708-4da6-aa05-7cbc33b44b7e",
                "_rev": "1-a2b6e5dac4e0447e7049c8c540b309d6",
                "_deleted": true
               }
             ]
    }