{"record":{"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/91a8b34c6f44bd28c421364f8edadc9f324dddd9/samples/guide/src/main/java/okhttp3/recipes/PostMultipart.java#L37-L65","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Register your own Imgur API client at https://api.imgur.com/oauth2 and replace IMGUR_CLIENT_ID.","Confirm docs/images/logo-square.png exists relative to the working directory before running.","Read response.code() and the JSON error body Imgur returns.","Branch on the code so the error body is visible."],"exampleFix":"// before\nif (!response.isSuccessful()) throw new IOException(\"Unexpected code \" + response);\n\n// after\nif (!response.isSuccessful()) {\n  throw new IOException(\"imgur upload HTTP \" + response.code() + \": \" + response.body().string());\n}","handlingStrategy":"validation","validationCode":"// Use your own Imgur client ID; confirm the image file exists.\nif (!new File(\"docs/images/logo-square.png\").isFile()) throw new FileNotFoundException();\n...\nif (!response.isSuccessful()) { /* read Imgur JSON error */ return; }","typeGuard":"static boolean uploaded(Response r) { return r.isSuccessful() && r.body() != null; }","tryCatchPattern":"try {\n  // call\n} catch (IOException e) {\n  // 'Unexpected code' (HTTP status, e.g. 401 bad Client-ID) or transport\n}","preventionTips":["Register your own Imgur API client; never rely on the sample Client-ID.","Confirm the local image file exists relative to the working directory.","Read Imgur's JSON error body on failure.","Inspect response.code() before treating the upload as successful."],"tags":["okhttp","http-status","imgur","multipart-upload","api-key","java"],"backgroundTag":null,"analyzedSha":"91a8b34c6f44bd28c421364f8edadc9f324dddd9","analyzedAt":"2026-08-10T18:39:54.316Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}