By Ian Bramford
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!
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 += "}";
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.