From: Peng Yu <pen...@gm...> - 2011-10-30 17:16:04
|
Hi, I modified samples/http/put.py slightly to try to put a xml file with authentication. But I got an error and the error message is not very helpful, and I don't see an example on authentication on http://localhost:8080/exist/devguide_rest.xml. I also tried samples/http/httpclient.pl, in which the user and password is in the URL. And it works. Then I tried to add user and pass word in the argument of httplib.HTTP(), but it doesn't work. $URL = "http://$USER:$PASS\@localhost:8080/exist/rest"; I think that the fix should be straightforward. Could any expert let me know? ~/linux/bin/src/eXist/eXistPut/main$ ./main.sh reading file catalog.xml ... catalog.xml storing document to collection /db/XQuery_Walmsley2007.ex/chapter01 ... An error occurred: Created ~/linux/bin/src/eXist/eXistPut/main$ cat main.sh #!/usr/bin/env bash ../eXistPut.py /db/XQuery_Walmsley2007.ex/chapter01 catalog.xml ~/linux/bin/src/eXist/eXistPut/main$ cat ../eXistPut.py #!/usr/bin/env python import httplib import sys import string import base64 collection = sys.argv[1] file = sys.argv[2] f = open(file, 'r') print "reading file %s ..." % file xml = f.read() f.close() p = string.rfind(file, '/') if p > -1: doc = file[p+1:] else: doc = file print doc print "storing document to collection %s ..." % collection con = httplib.HTTP('localhost:8080') userid='admin' passwd='FyDi3QfBON' auth = 'Basic ' + string.strip(base64.encodestring(userid + ':' + passwd)) con.putrequest('PUT', '/exist/rest/%s/%s' % (collection, doc)) con.putheader('Authorization', auth) con.putheader('Content-Type', 'text/xml') clen = len(xml) con.putheader('Content-Length', `clen`) con.endheaders() con.send(xml) errcode, errmsg, headers = con.getreply() if errcode != 200: f = con.getfile() print 'An error occurred: %s' % errmsg f.close() else: print "Ok." ~/linux/bin/src/eXist/eXistPut/main$ cat catalog.xml <!-- XQuery by Priscilla Walmsley (c) O'Reilly Media 2007 --> <!-- Product catalog input document--> <catalog> <product dept="WMN"> <number>557</number> <name language="en">Fleece Pullover</name> <colorChoices>navy black</colorChoices> </product> <product dept="ACC"> <number>563</number> <name language="en">Floppy Sun Hat</name> </product> <product dept="ACC"> <number>443</number> <name language="en">Deluxe Travel Bag</name> </product> <product dept="MEN"> <number>784</number> <name language="en">Cotton Dress Shirt</name> <colorChoices>white gray</colorChoices> <desc>Our <i>favorite</i> shirt!</desc> </product> </catalog> -- Regards, Peng |