Question

Getting a 400 response code on droplet create

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

	}

}



Submit an answer

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!

Sign In or Sign Up to Answer

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.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

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