Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8778663
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T19:35:17+00:00 2026-06-13T19:35:17+00:00

I have a node console app where I am trying to post a number

  • 0

I have a node console app where I am trying to post a number of record to the bulk document api on a local CouchDB instance and getting an “Invalid UTF-8 JSON.” What makes this particularly odd, is that I’m generating my JSON using JSON.stringify on a object literal.

I tested the actual json at http://jsonlint.com/ and supposedly it’s valid. I can’t post the full json here as it’s names and number from a directory, but I can show you a mock record to give you the basic structure.

{
 "family" : "Smith",
        "people":{
            "Bob":{
                 "name":"Bob", 
                 "active" : true,
                "birthday" : "1/01"
            },
            "Sue":{
                "name": "Sue", 
                "active" : true,
                "birthday" : "1/01"
            }
        },
        "address": {
            "street" :"1111 Cool Road",
            "city" : "Cincinnati",
            "state" : "OH",
            "zip" : "11111"
        },
        "phone" : "923-4908"            
};

I wrapped my array of records in a json object with the property “docs” as specified here. Is there anything obviously wrong with what I’m trying to do?

UPDATE:
Following lambmj’s suggestion, I wrote the string generated by node out to a file and then used curl to post that to the bulk document upload. That worked perfectly. I’d still like someone to help me correct my node code though, so that it works from within node. Here’s the exact code that builds and posts the request

  //The result variable is my array of 'family' objects, 
  //It's generated by parsing a text file, not show here
  var postOptions = {
  host: 'localhost',
  port: '5984',
  path: '/members/_bulk_docs',
  method: 'POST',
  headers: {
      'Content-Type': 'application/json',
      'Content-Length': result.length
  }
};
var request = http.request(postOptions, function(res){
    res.setEncoding('utf8');
    res.on('data', function (chunk) {
      console.log('Response: ' + chunk);
    });
});
var body = { "docs": result};
var stringData = JSON.stringify(body);
console.log(stringData);
request.write(stringData);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T19:35:18+00:00Added an answer on June 13, 2026 at 7:35 pm

    I was able to get your example to work using the following command:

    curl -X POST -H "Content-type:application/json" \
        http://localhost:5984/test/_bulk_docs -d @family.json
    

    where family.json contains the following. I added a second family so that there would more than one element in the array.

    {"docs": [
      {"family" : "Smith",
        "people":{
            "Bob":{
                 "name":"Bob", 
                 "active" : true,
                "birthday" : "1/01"
            },
            "Sue":{
                "name": "Sue", 
                "active" : true,
                "birthday" : "1/01"
            }
        },
        "address": {
            "street" :"1111 Cool Road",
            "city" : "Cincinnati",
            "state" : "OH",
            "zip" : "11111"
        },
        "phone" : "923-4908"
      },
      {"family" : "Jones",
        "people":{
            "John":{
                 "name":"John",
                 "active" : true,
                "birthday" : "1/01"
            },
            "Mary":{
                "name": "Mary",
                "active" : true,
                "birthday" : "1/01"
            }
        },
        "address": {
            "street" :"1112 Cool Road",
            "city" : "Cincinnati",
            "state" : "OH",
            "zip" : "11111"
        },
        "phone" : "923-4909"
      }
    ]}
    

    The JSON is the same as provided wrapped in an array and provided as the value for docs attribute of the JSON passed to CouchDB. Also there’s a trailing semi-colon (;) in the example you gave. That might be the problem if everything else is formatted correctly.

    UPDATE:

    OK, here’s the answer node code. I made 3 changes as noted in the code:

    #!/usr/bin/env node
    
    var http = require("http");
    
    // Embedding the family object here so that this test program will compile/run.
    
    result = {
         "family" : "Smith",
                "people":{
                    "Bob":{
                         "name":"Bob",
                         "active" : true,
                        "birthday" : "1/01"
                    },
                    "Sue":{
                        "name": "Sue",
                        "active" : true,
                        "birthday" : "1/01"
                    }
                },
                "address": {
                    "street" :"1111 Cool Road",
                    "city" : "Cincinnati",
                    "state" : "OH",
                    "zip" : "11111"
                },
                "phone" : "923-4908"        
        };
    
    var body = { "docs": [result]};                 // Change #1: docs takes an array.
    
    var stringData = JSON.stringify(body);          // Change #2: stringify first.
    
    var postOptions = {
      host: 'localhost',
      port: '5984',
      path: '/members/_bulk_docs',
      method: 'POST',
      headers: {
          'Content-Type': 'application/json',
          'Content-Length': stringData.length       // Change #3: send the length of the stringified data.
      }
    };
    
    var request = http.request(postOptions, function(res){
        res.setEncoding('utf8');
        res.on('data', function (chunk) {
          console.log('Response: ' + chunk);
        });
    });
    
    console.log(stringData);
    
    request.write(stringData);
    
    request.end();
    

    Here’s the output I get from node:

    {"docs":[{"family":"Smith","people":{"Bob":{"name":"Bob","active":true,"birthday":"1/01"},"Sue":{"name":"Sue","active":true,"birthday":"1/01"}},"address":{"street":"1111 Cool Road","city":"Cincinnati","state":"OH","zip":"11111"},"phone":"923-4908"}]}
    
    Response: [{"ok":true,"id":"721b8cde7a1e22b4cc106adbb3f41df9","rev":"1-306b71ff83df48c588a174fd5feafa34"}]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm have a js file that I'm trying to use in a node.js app.
I'm writing a node.js app, and I'm trying to read input from the console.
Currently i have a node.js and socket.io application in development on my local machine
I have a simple Node.js Rest server with a single POST service using Restify.
I have Node.js installed on a RedHat instance of EC2. I also installed Express
I have a basic node.js app that is designed to open up a connection
I'm trying to find the most elegant way for my node.js app to die
I have a little Node.js application that I'd like to hit a remote API
I have a nodejs app using bodyparser(), and this route : app.post('/users', function(req, res){
I have set a node app up on my server and i can ssh

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.