ianbr
By:
ianbr

Getting a 400 response code on droplet create

June 9, 2015 1k views
DigitalOcean Java Ubuntu

While I know there is a community Java based API I have been trying to create a droplet via my own simple app but receive a 400 response code. Please could someone tell me what's wrong with my code.

package code;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;

public class CreateDroplet {

    public static void main(String[] args) {
        String urlString = "https://api.digitalocean.com/v2/droplets";
          String newDroplet = "{'name' : 'ic2gb1'";
          newDroplet += ", 'region' : 'lon1'";
          newDroplet += ", 'size' : '2gb'";
          newDroplet += ", 'image' : 12079764";
          newDroplet += ", 'ssh_keys' : null";
          newDroplet += ", 'backups' : true";
          newDroplet += ", 'ipv6' : false";
          newDroplet += ", 'user_data' : null";
          newDroplet += ", 'private_networking' : false";
                newDroplet += '}';
        URL url;
        try {
            url = new URL(urlString );
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("POST");
            conn.setDoOutput(true);
            conn.setRequestProperty ("Authorization", "Bearer *****************************************************************");
            conn.setRequestProperty("Content-Type", "application/json");
            DataOutputStream writer = new DataOutputStream(conn.getOutputStream());

            writer.writeBytes(newDroplet);
            writer.flush();
            int responseCode = conn.getResponseCode();
            System.out.println("responseCode: + responseCode));
            String line;
            BufferedReader reader = new BufferedReader(new 
                                             InputStreamReader(conn.getInputStream()));
            while ((line = reader.readLine()) != null) {
              System.out.println(line);
            }
            writer.close();
            reader.close();
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}


1 Answer

Please ignore this question.
The problem turned out to be the structure of the json being sent.
This is what it should have looked like:

          String newDroplet = "{\"name\" : \"ic2gb1\"";
          newDroplet += ", \"region\" : \"lon1\"";
          newDroplet += ", \"size\" : \"2gb\"";
          newDroplet += ", \"image\" : 12079764";
          newDroplet += ", \"ssh_keys\" : null";
          newDroplet += ", \"backups\" : true";
          newDroplet += ", \"ipv6\" : false";
          newDroplet += ", \"user_data\" : null";
          newDroplet += ", \"private_networking\"   : false";
          newDroplet += "}";
Have another answer? Share your knowledge.