{"id":"aff6f51d4f6c3590","repo":"square/okhttp","slug":"unexpected-code-aff6f5","errorCode":null,"errorMessage":"Unexpected code ","messagePattern":"Unexpected code ","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"samples/guide/src/main/java/okhttp3/recipes/PostMultipart.java","lineNumber":55,"sourceCode":"  public void run() throws Exception {\n    // Use the imgur image upload API as documented at https://api.imgur.com/endpoints/image\n    RequestBody requestBody = new MultipartBody.Builder()\n        .setType(MultipartBody.FORM)\n        .addFormDataPart(\"title\", \"Square Logo\")\n        .addFormDataPart(\"image\", \"logo-square.png\",\n            RequestBody.create(\n                new File(\"docs/images/logo-square.png\"),\n                MEDIA_TYPE_PNG))\n        .build();\n\n    Request request = new Request.Builder()\n        .header(\"Authorization\", \"Client-ID \" + IMGUR_CLIENT_ID)\n        .url(\"https://api.imgur.com/3/image\")\n        .post(requestBody)\n        .build();\n\n    try (Response response = client.newCall(request).execute()) {\n      if (!response.isSuccessful()) throw new IOException(\"Unexpected code \" + response);\n\n      System.out.println(response.body().string());\n    }\n  }\n\n  public static void main(String... args) throws Exception {\n    new PostMultipart().run();\n  }\n}\n","sourceCodeStart":37,"sourceCodeEnd":65,"githubUrl":"https://github.com/square/okhttp/blob/4fc083138014aba3d0078f5c26d1ce84815fa984/samples/guide/src/main/java/okhttp3/recipes/PostMultipart.java#L37-L65","documentation":"Recipe-level guard after POSTing a multipart image upload to Imgur with a Client-ID authorization header. If the response is non-2xx, java.io.IOException(\"Unexpected code \" + response) is thrown. The most common real-world cause is Imgur rejecting the request (auth, quota, bad payload), since the recipe uses a shared demo client ID. If docs/images/logo-square.png is missing, the RequestBody.create(file, ...) call throws FileNotFoundException earlier, not here.","triggerScenarios":"POST https://api.imgur.com/3/image with MultipartBody.FORM containing title + image file, Authorization: Client-ID 9199fdef135c122. Fires on 401/403 (Client-ID revoked or rate-limited — Imgur caps demo keys), 400 (missing/invalid image bytes or wrong field name), 429 (per-app ratelimit), or 503.","commonSituations":"Using the shared demo Client-ID (9199fdef135c122) which has been throttled/revoked because everyone copies the recipe; the local image file path being wrong (→ earlier FileNotFoundException); Imgur API v3 rate limits (≈1,250/day for anonymous uploads); missing/invalid mime type on the part.","solutions":["Register your own Imgur application at https://api.imgur.com/oauth2 and use your own Client-ID.","Inspect response.code() and the JSON error body (Imgur returns {data:{error:...}, success:false}) — read response.body().string() in the error branch.","Confirm the image file exists at an absolute path before posting.","Set an explicit MediaType and filename in addFormDataPart."],"exampleFix":"// before\n.header(\"Authorization\", \"Client-ID \" + IMGUR_CLIENT_ID)\n...\ntry (Response response = client.newCall(request).execute()) {\n  if (!response.isSuccessful()) throw new IOException(\"Unexpected code \" + response);\n  ...\n}\n\n// after\n.header(\"Authorization\", \"Client-ID \" + MY_REGISTERED_CLIENT_ID)\n...\ntry (Response response = client.newCall(request).execute()) {\n  if (!response.isSuccessful()) {\n    String body = response.body().string();\n    throw new IOException(\"Imgur \" + response.code() + \": \" + body);\n  }\n  ...\n}","handlingStrategy":"validation","validationCode":"// Validate inputs and use your own registered Imgur Client-ID\nFile image = new File(context, \"docs/images/logo-square.png\");\nif (!image.isFile()) throw new FileNotFoundException(image.getAbsolutePath());\nString clientId = System.getenv(\"IMGUR_CLIENT_ID\"); // your own key, not the demo one\nif (clientId == null) throw new IllegalStateException(\"IMGUR_CLIENT_ID not set\");\nRequestBody body = new MultipartBody.Builder().setType(MultipartBody.FORM)\n    .addFormDataPart(\"image\", image.getName(),\n        RequestBody.create(image, MediaType.get(\"image/png\")))\n    .build();\nRequest request = new Request.Builder()\n    .url(\"https://api.imgur.com/3/image\")\n    .header(\"Authorization\", \"Client-ID \" + clientId)\n    .post(body).build();","typeGuard":null,"tryCatchPattern":"try (Response response = client.newCall(request).execute()) {\n  if (!response.isSuccessful()) {\n    String errBody = response.body().string();\n    if (response.code() == 401 || response.code() == 403)\n      throw new AuthException(\"Imgur Client-ID invalid/revoked: \" + errBody);\n    if (response.code() == 429)\n      throw new RateLimitedException(\"Imgur cap reached: \" + errBody);\n    throw new HttpException(response.code(), errBody);\n  }\n}","preventionTips":["Register your own Imgur application and read the Client-ID from config/env.","Read response.body().string() in the error branch — Imgur returns structured JSON errors.","Confirm the image file exists at an absolute path.","Watch the per-app daily upload cap."],"tags":["okhttp","http-status","multipart-upload","imgur","post","rate-limit","java"],"analyzedSha":"4fc083138014aba3d0078f5c26d1ce84815fa984","analyzedAt":"2026-08-04T19:09:04.639Z","schemaVersion":2}