0

I am implementing an Android app that should upload data to CouchDB. Since I have restricted the admin access to one account, I have to authenticate before inserting a new database. And this is what I am currently struggling with: Authenticate and insert a new database. Operating via Terminal and using curl, everything is working out fine the following way:

> curl -X PUT http://admin_name:admin_password@url:port/database_to_be_inserted

First approach

My first approach was to simply do the same via HTTP PUT in my code like that:

private boolean putJSON(String json, String url) {

    // url = http://admin_name:admin_password@url:port/database_to_be_inserted
    HttpClient client = new DefaultHttpClient();
    HttpPut put       = new HttpPut(url);

    try {
        StringEntity stringEntity = new StringEntity(json,"utf-8");
        put.setEntity(stringEntity);
        put.setHeader("Content-type", "application/json; charset=utf-8");
        put.setHeader("Accept", "application/json");

        HttpResponse response = client.execute(put);
        // ... buffered input reading on response...

        return true;
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    }       
}

However, doing so I retrieve the following error and JSON array:

Authentication error: Unable to respond to any of these challenges: {} {"error":"unauthorized","reason":"You are not a server admin."}

The point is, that using the same method for inserting a new user works out perfectly. So, if I am using the above method with a correctly formatted user JSON-Dictionary and the following url, the user is inserted correctly.

http://admin_name:admin_password@url:port/_users/org.couchdb.user:user_name

This should prove, that I am using the right admin data at least, shouldn't it?

Second approach

So, by now, I am trying to authenticate using the "Authorization" option in my HTTP PUT's header:

private boolean putDatabase(String userName, String password, String url) {

    // url = "http://url:port/database_to_be_inserted"
    HttpClient client = new DefaultHttpClient();
    HttpPut put = new HttpPut(url);

    String authenticationData = userName+":"+password;
    String encoding =  Base64.encodeToString(authenticationData.getBytes(Charset.forName("utf-8")), Base64.DEFAULT);
    put.setHeader("Authorization", "Basic " + encoding);

    try {
        put.setHeader("Content-type", "application/json; charset=utf-8");
        put.setHeader("Accept", "application/json");

        HttpResponse response = client.execute(put);
        // ... buffered input reading on response...

        return true;
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    }       
}

Still no success in inserting the database. The response I am parsing says:

Host not found

I have double checked the admin name, password, and url and everything seems correct. Does anyone of you see why this might not work out?

1 Answer 1

0

Ok, the answer is simple: The above code (at least the one of my second approach) is working fine. My mistake was to not explicitly specify the port via which the CouchDB should be accessed. This is, how I accidentally called the method:

putDatabase("adminName", "adminPassword", "http://url/database_to_be_inserted");

However, this is how I should have called it:

putDatabase("adminName", "adminPassword", "http://url:port/database_to_be_inserted");

Who is using iriscouch like me and does not know which port to specify here, can look it up in the config file. Using Futon this can be found in the entry "httpd > port" here:

> http://your_url_spec.iriscouch.com/_utils/config.html

More general and without Futon this can be found (and if you wish so edited) via command-line in the local.ini of your own CouchDB installation:

~$ cat etc/couchdb/local.ini

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.