post-url in place of curl with couchdb?

Guiserver, GTK-server, OpenGL, PostScript,
HTML 5, MIDI, IDE

post-url in place of curl with couchdb?

Postby jazper » Tue Apr 03, 2012 7:33 pm

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
jazper
 
Posts: 91
Joined: Thu Dec 10, 2009 8:26 am
Location: South Africa

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

Postby Ryon » Tue Apr 03, 2012 9:05 pm

Use the sleep function in the repository.
Ryon
 
Posts: 248
Joined: Thu Sep 26, 2002 12:57 am

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

Postby jazper » Wed Apr 04, 2012 7:46 pm

(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.
jazper
 
Posts: 91
Joined: Thu Dec 10, 2009 8:26 am
Location: South Africa

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

Postby Lutz » Wed Apr 04, 2012 8:45 pm

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.
Lutz
 
Posts: 5258
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California

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

Postby Ryon » Wed Apr 04, 2012 9:45 pm

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.
Ryon
 
Posts: 248
Joined: Thu Sep 26, 2002 12:57 am

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

Postby jazper » Thu Apr 05, 2012 8:57 pm

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
jazper
 
Posts: 91
Joined: Thu Dec 10, 2009 8:26 am
Location: South Africa

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

Postby jazper » Fri Apr 06, 2012 11:09 am

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.
jazper
 
Posts: 91
Joined: Thu Dec 10, 2009 8:26 am
Location: South Africa

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

Postby jazper » Fri Apr 06, 2012 11:43 am

Oops. Spoke too soon. It does not work when admin is set. But creates databases fine when no admin is set.
jazper
 
Posts: 91
Joined: Thu Dec 10, 2009 8:26 am
Location: South Africa

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

Postby Lutz » Fri Apr 06, 2012 4:53 pm

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').
Lutz
 
Posts: 5258
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California

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

Postby Kirill » Sat Apr 07, 2012 11:21 pm

There are curl bindings for newLISP you might want to try: https://gist.github.com/1119771
Kirill
 
Posts: 75
Joined: Wed Oct 31, 2007 1:21 pm

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

Postby jazper » Sun Apr 08, 2012 9:32 pm

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.
jazper
 
Posts: 91
Joined: Thu Dec 10, 2009 8:26 am
Location: South Africa


Return to newLISP Graphics & Sound

Who is online

Users browsing this forum: No registered users and 1 guest