Skip to content

Instantly share code, notes, and snippets.

@Stanley
Created January 16, 2011 16:45
Show Gist options
  • Save Stanley/781937 to your computer and use it in GitHub Desktop.
Save Stanley/781937 to your computer and use it in GitHub Desktop.
var http = require('http'),
couchdb = http.createClient(5984, 'localhost'),
uuid = require('node-uuid');
var id = uuid().replace(/-/g, '');
var request = couchdb.request('PUT', '/test/'+id +'?batch=ok', {
"host": "localhost",
"Content-Type": "multipart/related;boundary=\"frontier\""
});
var content = new Buffer("kolor:\r\nŻół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