Report this

What is the reason for this report?

Getting a 400 response code on droplet create

Posted on June 9, 2015

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();
		}

	}

}




This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

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 += "}";

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.