I am using couchdb-python to access my CouchDB backend. Sometimes I want to know the URL that the library is accessing, in order to use curl
or any other tool to access exactly the same URL. That is, I want to be able to copy/paste the encoded URL and perform a curl access, without manually editing anything.
Since couchdb-python
is not giving me this information, I have built a small tool to get the URL:
import urllib
def get_couchdb_url(server, database, design_doc, view_name, **options):
prefix = 'http://%s:%d' % (server, 5984)
url = '/'.join([prefix, database, "_design", design_doc, '_view', view_name])
params = urllib.urlencode(options)
if params != "" : url += "?" + params
return url
number='+666666666'
print get_couchdb_url('localhost', 'phonenumbers', 'control', 'allgeonums', key = number)
This gives me the following URL:
http://localhost:5984/phonenumbers/_design/control/_view/allgeonums?key=%2B666666666
Which, if I access with curl, gives:
curl -X GET 'http://localhost:5984/phonenumbers/_design/control/_view/allgeonums?key=%2B666666666'
{"error":"bad_request","reason":"invalid_json"}
If I manually edit, the URL, adding %22 before and after the key value, CouchDB accepts it and gives me the right answer:
curl -X GET 'http://localhost:5984/phonenumbers/_design/control/_view/allgeonums?key=%22%2B666666666%22'
Now I have two questions:
- Can
couchdb-python
show me the URL it is accessing? - How can I tell urllib.urlencode to produce encoded data which can be accepted by CouchDB? According to the manual, the values must be url encoded. I am not sure how to do this, for the general case. I would like to avoid having to read the
couchdb-python
sources for just this simple problem.