post-url in place of curl with couchdb?

Started by jazper, April 03, 2012, 12:33:15 PM

Previous topic - Next topic

jazper

Hello:

I'm trying to figure out how to write to couchdb using REST:  command line syntax for creating a database is:


curl -X PUT http://127.0.0.1:5984/mydatabase

With newLISP, will post-url achieve the same thing?  I have tried, but there's no response, and the database does not show up in Futon.  Can someone help, please?



regards

Ryon

#1
Use the sleep function in the repository.
\"Give me a Kaypro 64 and a dial tone, and I can do anything!\"

jazper

#2
(sleep) hasn't helped yet.  I tried this (attempting to create "mydatabase") with couchdb server running:


(set 'the-db {http://admin:password@localhost:5984/mydatabase})
(post-url the-db {} {8000})
(sleep 8000)
(exit)


the response to the above was:
Quote"ERR: DNS resolution failed"


If the admin:password bit is left out, the response is the same.

Lutz

#3
It's probably the format for passing parameters. Try this:


(post-url "http://localhost:5984/mydatabase"
"user=admin&password=password"
"application/x-www-form-urlencoded" 8000)


It's the second string passing variables, where you need the correct variable names. Perhaps the curl utility can help you to find out the correct format.



And of course the correct format in the last string is important too.



Could also be that 'localhost" isn't known by your machine, try "127.0.0.1" instead.

Ryon

#4
Quote from: "jazper"
(sleep) hasn't helped yet.  ...


I misunderstood. I thought you were trying to curl up on your futon couch for some REST this April.
\"Give me a Kaypro 64 and a dial tone, and I can do anything!\"

jazper

#5
Thanks, I will try your suggestion.  I had a great chuckle at your notion of me curling up and getting some REST this April.  There will be none of that for me until I get this right!



Further to the last experience:  I deleted the admin = secretpassword from /etc/couchdb/local.ini, so that "everyone is admin" in Futon.  After that, the following worked fine:


(set 'the-db {http://localhost:5984/the_hucklebuck})
(put-url the-db {})
(exit)


However, when I once again set an admin, I got the same error as before.  Evidently, it doesn't like the username password thing in the URL, making me even more keen to try your suggestion tomorrow.



thanks again

jazper

#6
After re-entering an admin, this worked:


(set 'the-db "http://127.0.0.1:5984/mynewdbthree")
(set 'auth  "user=my_admin_name&password=my_password" )
(set 'encode "application/x-www-form-urlencoded" )
(put-url the-db auth encode 8000)
(exit)


Many thanks to Ryon.

jazper

#7
Oops.  Spoke too soon.  It does not work when admin is set.  But creates databases fine when no admin is set.

Lutz

#8
Try to make it work using the curl utility first. It has a --verbose option which will print out the header its ius sending. From there then you take the correct format for authorization and encoding string in your 'post-url'  or 'put-url' command.



Also, perhaps you should use 'put-url' instead of 'post-url', becuase you curl command specifies PUT. Add the --verbose - probably as first option - and see what happens.



Also, in both 'post-url' and 'put-url', you can specify special header options shown by curl --verbose mode. Perhaps the authorization parameters go into the header not, the pots/put contents.



Make shure that each line in the header section finishes with rn.E.g:


"Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==rn"

This or something similar would go after the timeout number in 'put-url' or 'post-url' as last parameter. If the authentication strings needs to be Base64 encoded, your can use 'base64-enc'. If you have more than one line, they all go into the same string, but each finished with rn.



newLISP will always finish the header with "Connection: closern" no matter if you used the header option or not. If you don't specify any header option ist will also add "User-Agent: newLISP v10.4.0rn".



I also imagine that the encoding string will contain something different (only needed in 'post-url').

Kirill

#9
There are curl bindings for newLISP you might want to try: https://gist.github.com/1119771">https://gist.github.com/1119771

jazper

#10
Thanks for these tips, Lutz.  I did indeed end up using put-url.  The curl -v tip is a good one.  I saw the option, and just somehow neglected to invoke it.  And thanks too to Kirill.  I was not aware of the curl newlisp libary.  I will soldier on with these suggestions, and see how things go.