I want to make a POST request to get token from Openstack. I am able to do so using an addon on Mozilla by entering url:"http://*******/v2.0/tokens" and data as
{
"auth": {
"tenantName": "admin",
"passwordCredentials": {
"username": "xxxxxx",
"password": "xxxxxx"
}
}
}
How to do same by JAVA program? Till now I have tried the following code, but with no success.
package rest.openstack;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class NetClientGet {
// http://localhost:8080/RESTfulExample/json/product/get
public static void main(String[] args) {
try {
URL url = new URL("http://***.**.**.**:5000/v2.0/tenants/"); //url for openstack
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "application/json");
if (conn.getResponseCode() != 200) {
throw new RuntimeException("Failed : HTTP error code : "
+ conn.getResponseCode());
}
BufferedReader br = new BufferedReader(new InputStreamReader(
(conn.getInputStream())));
String output;
System.out.println("Output from Server .... \n");
while ((output = br.readLine()) != null) {
System.out.println(output);
}
conn.disconnect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
I prefer Jersey API for REST calls;
Below one is to obtain token for specified user that is in your POST entity;
Then you can parse your entity with some of parser such as com.google.gson.JsonParser.
After that for each request , you need to apply X-Auth-Token to your REST header in order to authenticate for services.