Page 1 of 1

post-url in place of curl with couchdb?

Posted: Tue Apr 03, 2012 7:33 pm
by jazper
Hello:
I'm trying to figure out how to write to couchdb using REST: command line syntax for creating a database is:

Code: Select all

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

Re: post-url in place of curl with couchdb?

Posted: Tue Apr 03, 2012 9:05 pm
by Ryon
Use the sleep function in the repository.

Re: post-url in place of curl with couchdb?

Posted: Wed Apr 04, 2012 7:46 pm
by jazper
(sleep) hasn't helped yet. I tried this (attempting to create "mydatabase") with couchdb server running:

Code: Select all

(set 'the-db {http://admin:password@localhost:5984/mydatabase})
(post-url the-db {} {8000})
(sleep 8000)
(exit)
the response to the above was:
"ERR: DNS resolution failed"
If the admin:password bit is left out, the response is the same.

Re: post-url in place of curl with couchdb?

Posted: Wed Apr 04, 2012 8:45 pm
by Lutz
It's probably the format for passing parameters. Try this:

Code: Select all

(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.

Re: post-url in place of curl with couchdb?

Posted: Wed Apr 04, 2012 9:45 pm
by Ryon
jazper wrote: (sleep) hasn't helped yet. ...
I misunderstood. I thought you were trying to curl up on your futon couch for some REST this April.

Re: post-url in place of curl with couchdb?

Posted: Thu Apr 05, 2012 8:57 pm
by jazper
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:

Code: Select all

(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

Re: post-url in place of curl with couchdb?

Posted: Fri Apr 06, 2012 11:09 am
by jazper
After re-entering an admin, this worked:

Code: Select all

(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.

Re: post-url in place of curl with couchdb?

Posted: Fri Apr 06, 2012 11:43 am
by jazper
Oops. Spoke too soon. It does not work when admin is set. But creates databases fine when no admin is set.

Re: post-url in place of curl with couchdb?

Posted: Fri Apr 06, 2012 4:53 pm
by Lutz
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 \r\n.E.g:

Code: Select all

"Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==\r\n"
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 \r\n.

newLISP will always finish the header with "Connection: close\r\n" 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.0\r\n".

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

Re: post-url in place of curl with couchdb?

Posted: Sat Apr 07, 2012 11:21 pm
by Kirill
There are curl bindings for newLISP you might want to try: https://gist.github.com/1119771

Re: post-url in place of curl with couchdb?

Posted: Sun Apr 08, 2012 9:32 pm
by jazper
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.