Reading clear text data back from CouchBase

261 views
Skip to first unread message

Gareth

unread,
Jul 4, 2013, 10:09:55 AM7/4/13
to couchba...@googlegroups.com
Disclaimer: this particular issue is caused by me trying to be lazy and is not an issue I would have in production.

I'm trying to access a view from a script in my browser.  To avoid CORS issues (which it seems CouchBase does not support), I was trying to load my HTML and JavaScript files into CouchBase and then try and read them back directly from the browser, but I can't get this to work.

When I save the object into CouchBase, it (understandably) fails to recognize it as valid JSON and does some kind of compression/encryption/translation on it:


Then, when I try and recover the document, it gives me back a JSON version of the "compressed" form (it's described as base64 encoding, but I think it's base64 of some compression, but it's hard to tell):

HTTP/1.1 200 OK
X-Couchbase-Meta: {"id":"webui","rev":"2-00008c4196f76c460000000000000000","att_reason":"invalid_json","expiration":0,"flags":0,"type":"base64"}
Server: MochiWeb/1.0 (Any of you quaids got a smint?)
Date: Thu, 04 Jul 2013 14:01:02 GMT
Content-Type: application/json
Content-Length: 10
Cache-Control: must-revalidate

"Um9vdA=="

I've googled around a bit, and can't find any CouchBase 2 compliant way of "decrypting" this; I can't even find a description of what the exact algorithm is.  But since I want to be able to read it from the browser I can't do anything at all clever.  The only references I could find to this problem referenced the CouchDB "attachment" feature, but this is not supported in CouchBase 2:

{"error":"bad_request","reason":"attachments not supported in Couchbase"}

I tried specifying "Accept:" headers to see if that would force it to recognize I wanted text/plain or text/html but it happily returned me application/json.  I feel there should be some sort of option or URL or something that enables me to get back what I put in, but I can't find it.  Can anyone help me?

Thanks,
Gareth

Perry Krug

unread,
Jul 5, 2013, 2:20:44 PM7/5/13
to couchba...@googlegroups.com
Gareth, I'll try to help here.

First off, it's really recommended to access Couchbase through the use of one of our native SDK's.  This will give you not only the best performance and scale, but also a supported and long-time compatible way.  The URL-based access is really not a supported or recommended way of getting out individual documents.  

If you use the SDK, you will be guaranteed to get back what you put in.  To explain that a bit more, Couchbase "caches" data in its memcached-based layer in just a simple byte-array format.  As data is written to disk, it is parsed into JSON.  If it can be successfully parsed, it is sent through Google's "snappy" compression and stored on disk.  This compression is simply for space saving and does not at all change how your data looks.  If the document cannot be parsed as JSON, it is base64 encoded and stored as an "attachment" to a JSON document much in the same way that CouchDB's attachment capability works (though slightly differently).  When you want to retrieve one of these documents, through the memory caching layer, it will be pulled out of the attachment, decoded and sent back as a byte-array...exactly bit-for-bit the way you put it in.  However, if you are accessing it "around" this layer, it is still base64 encoded which is what you are seeing.

Is there some reason you can't use an SDK in this application?

Perry



--
Couchbase 2.0 is Here!: http://www.couchbase.com/download
Couchbase 2.0 Learn: http://www.couchbase.com/learn
Couchbase Forums: http://www.couchbase.com/forums
---
You received this message because you are subscribed to the Google Groups "Couchbase Team 8091" group.
To unsubscribe from this group and stop receiving emails from it, send an email to couchbase-809...@googlegroups.com.
To post to this group, send email to couchba...@googlegroups.com.
Visit this group at http://groups.google.com/group/couchbase-8091.
To view this discussion on the web visit https://groups.google.com/d/msgid/couchbase-8091/7360affe-2449-4708-b0da-2ed0d86d24c0%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Gareth Powell

unread,
Jul 10, 2013, 7:21:05 AM7/10/13
to couchba...@googlegroups.com
Thanks Perry.

As I said, it was really just laziness, and I was just expecting that there would be a way to get back what you put in.  Instead, I set up a forwarding apache server to work around the issues and served my (static) content just straight off the disk out of git.

Cheers,
Gareth


Reply all
Reply to author
Forward
0 new messages