Skip to content

Instantly share code, notes, and snippets.

@Stanley
Created January 16, 2011 12:07
Show Gist options
  • Save Stanley/781730 to your computer and use it in GitHub Desktop.
Save Stanley/781730 to your computer and use it in GitHub Desktop.
var http = require('http');
var couchdb = http.createClient(5984, 'localhost');
// PUT method works correctly
// var request = couchdb.request('PUT', '/test/test', {
// POST returns 415: {"error":"bad_content_type","reason":"Content-Type must be application/json"}
var request = couchdb.request('POST', '/test', {
"host": "localhost",
"Content-Type": "multipart/related;boundary=\"frontier\""
});
var content = new Buffer("Żółty");
var doc = {foo: "bar"};
doc._attachments = {
'test.txt': {
'follows': true,
'content_type': 'text/plain',
'length': content.length
}
}
request.end(
"--frontier\r\n" +
"content-type: application/json\r\n" +
"\r\n" +
JSON.stringify(doc) +
"\r\n--frontier\r\n" +
"\r\n" +
content +
"\r\n--frontier--"
);
request.on('response', function (response) {
console.log('STATUS: ' + response.statusCode);
console.log('HEADERS: ' + JSON.stringify(response.headers));
response.setEncoding('utf8');
response.on('data', function (chunk) {
console.log('BODY: ' + chunk);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment