Reputation: 523
I am trying to write a post curl in java. my curl is:
curl -X PUT -u username:password http://localhost:1234/api/2.0/data1/include/value1
I wrote in java:
String stringUrl = "http://localhost:1234/api/2.0/data1/include/value1";
URL url = new URL(stringUrl);
URLConnection uc = url.openConnection();
uc.setRequestProperty("X-Requested-With", "Curl");
String userpass = "username" + ":" + "password";
String basicAuth = "Basic " + new String(new Base64().encode(userpass.getBytes()));
uc.setRequestProperty("Authorization", basicAuth);
InputStreamReader inputStreamReader = new InputStreamReader(uc.getInputStream());
Interestingly, it did not give any error but nothing happened and value1 did not add to input 1 so it means the curl post that I wrote did not do anything. Can anyone be kind enough to help me convert the above post curl request to java code?
Upvotes: 0
Views: 1012
Reputation: 10305
For better invoking HTTP methods use Apache HttpClient.
Here is a nice overview how to start with get and post method: http://www.vogella.com/tutorials/ApacheHttpClient/article.html
Upvotes: 1
Reputation: 398
It looks like you forgot to call: uc.setDoOutput(true); before trying to set any http headers with setRequestProperty() See: http://docs.oracle.com/javase/tutorial/networking/urls/readingWriting.html
Upvotes: 0