square/okhttp · error · IOException

Unexpected code

Error message

Unexpected code 

What it means

Thrown in the PostMultipart recipe: `if (!response.isSuccessful()) throw new IOException("Unexpected code " + response)`. It POSTs a multipart image to Imgur with a hardcoded Client-ID. The most common real-world failure is Imgur's API rejecting the outdated sample client ID or the missing image file.

Solutions

  1. Register your own Imgur API client at https://api.imgur.com/oauth2 and replace IMGUR_CLIENT_ID.
  2. Confirm docs/images/logo-square.png exists relative to the working directory before running.
  3. Read response.code() and the JSON error body Imgur returns.
  4. Branch on the code so the error body is visible.

Example fix

// before
if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);

// after
if (!response.isSuccessful()) {
  throw new IOException("imgur upload HTTP " + response.code() + ": " + response.body().string());
}
Defensive patterns

Strategy: validation

Validate before calling

// Use your own Imgur client ID; confirm the image file exists.
if (!new File("docs/images/logo-square.png").isFile()) throw new FileNotFoundException();
...
if (!response.isSuccessful()) { /* read Imgur JSON error */ return; }

Type guard

static boolean uploaded(Response r) { return r.isSuccessful() && r.body() != null; }

Try / catch

try {
  // call
} catch (IOException e) {
  // 'Unexpected code' (HTTP status, e.g. 401 bad Client-ID) or transport
}

Prevention

When it happens

Trigger: POST https://api.imgur.com/3/image returns 401 (invalid/revoked Client-ID '9199fdef135c122'), 400 (the local file docs/images/logo-square.png does not exist, producing an empty/bad part), 429 (rate limit), or 5xx. Imgur has tightened API access and revoked old demo keys.

Common situations: Using the sample Client-ID which Imgur has disabled; running from a directory without docs/images/logo-square.png; not having registered your own Imgur API credentials.

Related errors


AI-assisted analysis of square/okhttp@91a8b34c6f (2026-08-10). Data as JSON: /api/errors/aff6f51d4f6c3590. Report an issue: GitHub.

Appendix: source

Thrown at samples/guide/src/main/java/okhttp3/recipes/PostMultipart.java:55

  public void run() throws Exception {
    // Use the imgur image upload API as documented at https://api.imgur.com/endpoints/image
    RequestBody requestBody = new MultipartBody.Builder()
        .setType(MultipartBody.FORM)
        .addFormDataPart("title", "Square Logo")
        .addFormDataPart("image", "logo-square.png",
            RequestBody.create(
                new File("docs/images/logo-square.png"),
                MEDIA_TYPE_PNG))
        .build();

    Request request = new Request.Builder()
        .header("Authorization", "Client-ID " + IMGUR_CLIENT_ID)
        .url("https://api.imgur.com/3/image")
        .post(requestBody)
        .build();

    try (Response response = client.newCall(request).execute()) {
      if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);

      System.out.println(response.body().string());
    }
  }

  public static void main(String... args) throws Exception {
    new PostMultipart().run();
  }
}

View on GitHub (pinned to 91a8b34c6f)