Thursday, April 15, 2010

Uploading Attachments with Node.CouchApp.js

‹prev | My Chain | next›

Last night I was unable to verify that I could upload attachments using a node.couchapp.js. Node.couchapp.js is a node.js reimplementation of couchapp, a framework for creating and maintaining CouchDB applications. When I explored couchapp a while back, I finished off with uploading attachments, which I considered a fairly advanced capability. If node.couchapp.js is capable of uploading attachments, then I will be satisfied that it is capable of writing serious CouchDB applications.

Unfortunately, last night did not go well. The CouchDB logs are no help, so I resort to packet sniffing:
cstrom@whitefall:~/repos/relax$ sudo tcpdump -i lo -n -s 0 -w - port 5984
Trying to upload with the new, node.couchapp.js version, this is what I sniff:
POST /seed/test HTTP/1.1
Host: localhost:5984
Connection: keep-alive
User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US) AppleWebKit/533.4 (KHTML, like Gecko) Chrome/5.0.371.0 Safari/533.4
Referer: http://localhost:5984/seed/_design/app/_show/update/test
Content-Length: 18379
Cache-Control: max-age=0
Origin: http://localhost:5984
Content-Type: multipart/form-data; boundary=----WebKitFormBoundarywZAoXOCTAcq980gf
Accept: application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3

------WebKitFormBoundarywZAoXOCTAcq980gf
Content-Disposition: form-data; name="_attachments"; filename="e_mummy_salmon_0008.jpg"
Content-Type: image/jpeg

... Image Data ...

------WebKitFormBoundarywZAoXOCTAcq980gf
Content-Disposition: form-data; name="_rev"

10-55c92e5c9b40e823615426e767264bcb
------WebKitFormBoundarywZAoXOCTAcq980gf--
The response from the server is:
HTTP/1.1 409 Conflict
Server: CouchDB/0.10.0 (Erlang OTP/R13B)
Date: Fri, 16 Apr 2010 00:58:02 GMT
Content-Type: text/plain;charset=utf-8
Content-Length: 58
Cache-Control: must-revalidate


{"error":"conflict","reason":"Document update conflict."}
Bah!

With the old, working couchapp version, this is what I see when I upload an image:
POST /eee/test HTTP/1.1
Host: localhost:5984
Connection: keep-alive
User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US) AppleWebKit/533.4 (KHTML, like Gecko) Chrome/5.0.371.0 Safari/533.4
Referer: http://localhost:5984/eee/_design/relax/_show/upload/test
Content-Length: 18379
Cache-Control: max-age=0
Origin: http://localhost:5984
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary3iardfLQJdZF1Q4L
Accept: application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3

------WebKitFormBoundary3iardfLQJdZF1Q4L
Content-Disposition: form-data; name="_attachments"; filename="e_mummy_salmon_0008.jpg"
Content-Type: image/jpeg

... Image Data ...

------WebKitFormBoundary3iardfLQJdZF1Q4L
Content-Disposition: form-data; name="_rev"

13-a88e99cdd18a1a30d50e3bd59c74b4cb
------WebKitFormBoundary3iardfLQJdZF1Q4L--
And the server replies with:
HTTP/1.1 201 Created
Server: CouchDB/0.10.0 (Erlang OTP/R13B)
Etag: "14-f2afcb2344fa446cb000af4db7c69bea"
Date: Fri, 16 Apr 2010 01:14:19 GMT
Content-Type: text/plain;charset=utf-8
Content-Length: 68
Cache-Control: must-revalidate

{"ok":true,"id":"test","rev":"14-f2afcb2344fa446cb000af4db7c69bea"}
I ediff these two requests and find them identical, aside from the DB, the content boundary and the revision IDs. Identical! What. The. Hell?

Not knowing what else to try, I upload an attachment to the document directly in the CouchDB futon interface. Happily, that also fails. I say happily because I had no idea what else to try at this point.

I still do not know what was wrong with this particular document. To try out upload in node.couchapp.js, I delete the test document with which I had been working. That does not quite work as the document hangs around with an earlier revision number. The second time that I delete it, it finally goes away. In retrospect, I should have investigated this a bit more (perhaps there was a conflict?), but I was grasping at straws. With the document really, really deleted, I retry the upload and:



So I had it working last night, but didn't know it. Not only didn't I know it, but I have not even learned what the problem was. Bummer. Ah well, at least I know that I can do fairly advanced things with node.couchapp.js.

Day #74

No comments:

Post a Comment