-1

How do I do a HTTP GET POST PUT DELETE Request using Java? I'm using CouchDB and I can post data using cUrl into the database. How do I do the same thing using Java however I cannot find any information on this with good documentation.

curl -X PUT http://anna:secret@127.0.0.1:5984/somedatabase/

Could some please change this cUrl request to Java. Otherwise please recommend me libraries to do so.

Thank You.

DaveR
  • 9,286
  • 2
  • 35
  • 58
shap4th
  • 129
  • 1
  • 4
  • Have you tried looking up `java post request`...? – Sotirios Delimanolis Mar 08 '14 at 02:13
  • Yes I did, but I don't understand it.. There is none with good documentation. If you could point me to a location with good documentation it would be great. – shap4th Mar 08 '14 at 02:28
  • [This one](http://stackoverflow.com/questions/4205980/java-sending-http-parameters-via-post-method-easily) is no good? How about [this one](http://stackoverflow.com/questions/3324717/sending-http-post-request-in-java)? And [here](http://stackoverflow.com/questions/4543894/android-java-http-post-request)? There are [so many](http://stackoverflow.com/questions/1359689/how-to-send-http-request-in-java). Probably the [best one](http://stackoverflow.com/questions/2793150/how-to-use-java-net-urlconnection-to-fire-and-handle-http-requests). – Sotirios Delimanolis Mar 08 '14 at 02:32

1 Answers1

0

You can use HttpClient by Apache.

Here is an example usage of how to call a POST request

String url = "https://your.url.to.post.to/";

HttpClient client = HttpClientBuilder.create().build();
HttpPost post = new HttpPost(url);


List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
urlParameters.add(new BasicNameValuePair("param1", "value1"));


post.setEntity(new UrlEncodedFormEntity(urlParameters));

HttpResponse response = client.execute(post);
System.out.println("Response Code : " 
                    + response.getStatusLine().getStatusCode());

BufferedReader rd = new BufferedReader(
        new InputStreamReader(response.getEntity().getContent()));

StringBuffer result = new StringBuffer();
String line = "";
while ((line = rd.readLine()) != null) {
    result.append(line);
}

I do recommend that you check this article for more examples.

deadlock
  • 8,894
  • 8
  • 44
  • 67
  • Where do I add user and pass? Please comment on the code, it would help me better. Thank You. – shap4th Mar 08 '14 at 12:37
  • @shap4th check the example in the article in my answer. See section `3. Apache HttpClient – Automate login Google ` – deadlock Mar 08 '14 at 12:41