0

I want to utilize https://api.imgbb.com/1/upload in order to upload Bitmaps onto ImageBB using HttpURLConnection as follows:

Upload an image:

  public JsonObject uploadImage(Bitmap bitmap) throws IOException, ExecutionException, InterruptedException {
    JsonObject JSONInput = new JsonObject();
    JSONInput.addProperty(
      "image", BASE_64_IMAGE_STRING
    );
    JsonObject response = new Requests().execute(JSONInput).get();
    return response;
  }

Requests Class:

public class Requests extends AsyncTask<JsonObject, JsonObject, JsonObject> {
  @Override
  protected JsonObject doInBackground(JsonObject... jsonObjects) {
    URL url = null;
    url = new URL("https://api.imgbb.com/1/upload?key=API_KEY&expiration=300");
    HttpURLConnection connection = null;
    
    connection = (HttpURLConnection) url.openConnection();
    
    connection.setRequestMethod("POST")
    connection.setRequestProperty("Content-Type", "application/json");
    connection.setDoOutput(true);

    OutputStream os = connection.getOutputStream()
      OutputStreamWriter osw = new OutputStreamWriter(os, StandardCharsets.UTF_8);
      osw.write(jsonObjects[0].toString());
      osw.flush();
      osw.close();

    connection.connect()
    String result = new BufferedReader(new InputStreamReader(connection.getErrorStream()))
      .lines().collect(Collectors.joining("\n"));
    StringBuilder response = new StringBuilder();
    BufferedReader br = new BufferedReader(
      new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8))
      String responseLine = null;
      while ((responseLine = br.readLine()) != null) {
        response.append(responseLine.trim());
     

    Gson gson = new GsonBuilder().setPrettyPrinting().create();
    JsonElement jelem = gson.fromJson(response.toString(), JsonElement.class);
    return jelem.getAsJsonObject();
  }

  @Override
  protected void onPostExecute(JsonObject jsonObject) {
    super.onPostExecute(jsonObject);
  }
}

The jsonObjects consists of a single element which is of the form {"image": base64_representation_of_image"}

However, I keep getting {"status_code":400,"error":{"message":"Empty upload source.","code":130},"status_txt":"Bad Request"} back from the API. I can't seem to figure out what I am doing wrong.

I can't find out any resources which use similar APIs in Android. Any direction would be appreciated.

I am trying to use the HttpURLConnection class in order to make API requests to ImageBB. However, it does not seem to recognise the base64 image string I am passing

1 Answer 1

0

The ImgBB API is not expecting the data being send as a JSON body. Instead the content should be send either with GET variables or with POST form field data. This means that you cannot use the content type application/json to send your data to the API as a JSON string.

You have to send a "normal" HTTP POST request with form field data, with the required fields image and key. Check other questions like Sending HTTP POST Request In Java how to send a HTTP POST request with form field data with java.

Sign up to request clarification or add additional context in comments.

2 Comments

If you could provide an example for how to do it using HttpURLConnection, that would be very helpful. The best I could find on that is this. Please note that I cant add the image parameter in the url since the base64 representations get very long
@Spongerooski Check other questions like stackoverflow.com/questions/34915556/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.