Unlock a world of possibilities! Login now and discover the exclusive benefits awaiting you.
try {
java.net.URL url = new java.net.URL("https://yourURL.com/api/v1/products/1/");//the 1 is the product id
java.net.HttpURLConnection conn = (java.net.HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("PUT");
conn.setRequestProperty("Content-Type", "application/json");
String username = "someUser";
String password = "somePassword";
sun.misc.BASE64Encoder enc = new sun.misc.BASE64Encoder();
String userpassword = username + ":" + password;
String encodedAuthorization = enc.encode( userpassword.getBytes() );
conn.setRequestProperty("Authorization", "Basic "+encodedAuthorization);
conn.setAllowUserInteraction(true);
String input = "{\"available\":\"14\",\"price\": \"14.99\"}";
java.io.OutputStream os = conn.getOutputStream();
os.write(input.getBytes());
os.flush();
os.close();
/*if (conn.getResponseCode() != java.net.HttpURLConnection.HTTP_CREATED) {
throw new RuntimeException("Failed : HTTP error code : "
+ conn.getResponseCode());
}
*/
java.io.BufferedReader br = new java.io.BufferedReader(new java.io.InputStreamReader(
(conn.getInputStream())));
String output;
System.out.println("Output from Server .... \n");
while ((output = br.readLine()) != null) {
System.out.println(output);
}
conn.disconnect();
}
catch (IOException e) {
e.printStackTrace();
}