How to setup administrators in Couchdb via Apache's HttpClient (given a curl example)

喜你入骨 提交于 2019-12-23 05:29:05

问题


So I am working on a CouchDB Gui Toolbox for easier maintaining an setting up CouchDB on Android, as Futon is quite uncomfortable on a small mobile device.

I wanted to stick to the "org.apache.http.client.*" packages for this which was working out quite well until I wanted to setup administrators..

With the commandline tool "curl" it works like a charm:

curl -X PUT http://127.0.0.1:5984/_config/admins/username -d '"password"'

But I keep on having big problems translating that to a "org.apache.http.client.methods.HttpPut()" method.

Any help appreciated.


回答1:


DefaultHttpClient client = new DefaultHttpClient();

HttpPut put = new HttpPut("http://127.0.0.1:5984/_config/admins/username");
put.setEntity(new StringEntity("\"password\""));
client.execute(put);



回答2:


Yes, sorry. Just to complete the answer, here's how to actually deal with the response I get for the request:

    DefaultHttpClient client = new DefaultHttpClient();
    JSONObject json = null;

    HttpPut put = new HttpPut("http://127.0.0.1:5984/_config/admins/username");

    try {
        StringEntity strEntity = new StringEntity("\"" + password + "\"");
        put.setEntity(strEntity);
        response = client.execute(put);
        HttpEntity responseEntity = response.getEntity();
    //Or do something with the entity of the response  
    // if (response.getStatusLine().getStatusCode() == 200) {
    //      return something;
    //  }
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();  
    } catch (IOException e) {
        e.printStackTrace();  
    }


来源:https://stackoverflow.com/questions/4188621/how-to-setup-administrators-in-couchdb-via-apaches-httpclient-given-a-curl-exa

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!