diff --git a/lib/couchdb.js b/lib/couchdb.js index 1c0cfbf..5fee569 100644 --- a/lib/couchdb.js +++ b/lib/couchdb.js @@ -143,6 +143,12 @@ exports.createClient = function(port, host, user, pass, maxListeners) { // CouchDB 1.0 requires application/json for _bulk_docs and other POST APIs. options.headers['Content-Type'] = options.headers['Content-Type'] || 'application/json'; + // Add content length, eg for nginx proxy servers + if (options.data && typeof options.data != 'string') { + options.data = exports.toJSON(options.data); + } + options.headers['Content-Length'] = options.headers['Content-Length'] || (options.data) ? Buffer.byteLength(options.data, 'utf8') : 0; + var request = httpClient.request( options.method.toUpperCase(), @@ -160,10 +166,6 @@ exports.createClient = function(port, host, user, pass, maxListeners) { request.addListener('error', onError); request.addListener('close', onClose); - if (options.data && typeof options.data != 'string') { - options.data = exports.toJSON(options.data); - } - if (options.data) { request.write(options.data, options.requestEncoding || 'utf8'); }