Create Blockchain Account with Java HttpURLConnection Post


#1

The following code dose create the blockchain account, but if I try to use the password I posted for let’s say creating a contract I get “incorrect password”. Any help on what I’m doing wrong is appreciated.

 public Response postBlockchainAccount() {
    try 
    {
        String url = "http://192.168.99.100/bloc/v2.2/users/testUser20";
        URL obj = new URL(url);
        HttpURLConnection conn = (HttpURLConnection) obj.openConnection();  
        conn.setRequestProperty("Content-Type", "application/json");
        conn.setRequestProperty("accept", "application/json;charset=utf-8");
        conn.setDoOutput(true);
        conn.setRequestMethod("POST");  
        String data =  "\"{\\\"password\\\":\\\"Password10\\\"}\"";
        OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream());
        out.write(data);
        out.close();
        BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        String s = "", line2 = null;
        while ((line2 = in.readLine()) != null) {
            s += line2;
        }
        return Response.ok(s).build();
    } catch (Exception e){
        System.out.println(e);
    }
    return null;
 }

#2

Figured it out.

Changed

conn.setRequestProperty("Content-Type", "application/json");

TO

conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

AND

String data = "\"{\\\"password\\\":\\\"Password10\\\"}\"";

TO

String data = "password=Password10";


#3

Glad to hear it’s resolved @psaban20 - I will add this to our FAQs.