Question: I try to upload image to imgur via code but I get 400 Bad Request, why and how can I fix?

Question

I try to upload image to imgur via code but I get 400 Bad Request, why and how can I fix?

Answers 0
Added at 2016-12-31 19:12
Tags
Question

I try to make a code that uploads automatically image to imgur Here is my code:

    File image = new File("[image path]");
    FileInputStream fis = new FileInputStream(image);
    byte byteArray[] = new byte[(int)image.length()];
    fis.read(byteArray);
    String imageString = Base64.encodeBase64String(byteArray);
    fis.close();
    URL imgururl = new URL("http://api.imgur.com/3/image");
    HttpURLConnection uploadtoimgur =  (HttpURLConnection)imgururl.openConnection();
    uploadtoimgur.setRequestMethod("POST");
    uploadtoimgur.setRequestProperty("Authorization", "Client-ID [my client id]");
    uploadtoimgur.setDoOutput(true);
    uploadtoimgur.getOutputStream().write(imageString.getBytes());
    System.out.println("Imgur Upload: "+uploadtoimgur.getResponseCode()+" "+uploadtoimgur.getResponseMessage());

I have developer and client id on imgur and I saw their api but I have no idea why it returns 400 bad request all the time... https://api.imgur.com/endpoints/image - here is their api. According to their Upload Image section on the endpoints, base64 data of the picture is enough, so what did I do wrong?

If I forgot to mention something, sorry about it just comment and I will respond. Thank you.

Answers to

I try to upload image to imgur via code but I get 400 Bad Request, why and how can I fix?

Source Show
◀ Wstecz