{"record":{"id":"5027c105b57a619a","repo":"square/okhttp","slug":"unexpected-code-5027c1","errorCode":null,"errorMessage":"Unexpected code ","messagePattern":"Unexpected code ","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"samples/guide/src/main/java/okhttp3/recipes/PostStreaming.java","lineNumber":61,"sourceCode":"        }\n      }\n\n      private String factor(int n) {\n        for (int i = 2; i < n; i++) {\n          int x = n / i;\n          if (x * i == n) return factor(x) + \" × \" + i;\n        }\n        return Integer.toString(n);\n      }\n    };\n\n    Request request = new Request.Builder()\n        .url(\"https://api.github.com/markdown/raw\")\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 PostStreaming().run();\n  }\n}\n","sourceCodeStart":43,"sourceCodeEnd":71,"githubUrl":"https://github.com/square/okhttp/blob/91a8b34c6f44bd28c421364f8edadc9f324dddd9/samples/guide/src/main/java/okhttp3/recipes/PostStreaming.java#L43-L71","documentation":"Thrown in the PostStreaming recipe: `if (!response.isSuccessful()) throw new IOException(\"Unexpected code \" + response)`. It streams generated markdown (a prime-factorization table) to GitHub's markdown/raw endpoint via a custom RequestBody whose writeTo writes to the sink on demand. The guard fires when GitHub answers non-2xx after the stream completes.","triggerScenarios":"POST https://api.github.com/markdown/raw with a streamed markdown body returns 404 (endpoint gone), 403 (rate limit), 400 (body too large or malformed), or 5xx. Because the body is streamed without Content-Length, some servers/proxies reject it; GitHub historically supports chunked markdown/raw but may change.","commonSituations":"GitHub deprecating markdown/raw; rate limit; a proxy that requires Content-Length rejecting the chunked POST; generating an unexpectedly huge body (primes up to 997 is small, but the pattern scales).","solutions":["Check response.code() and the body for GitHub's error message.","If a proxy rejects chunked uploads, buffer the body and set a Content-Length (use RequestBody.create with bytes).","Add a token header to avoid 403 rate limiting.","Confirm the markdown/raw endpoint still exists; switch to /markdown if needed."],"exampleFix":"// before\nif (!response.isSuccessful()) throw new IOException(\"Unexpected code \" + response);\n\n// after\nif (!response.isSuccessful()) {\n  throw new IOException(\"markdown/raw HTTP \" + response.code() + \": \" + response.body().string());\n}","handlingStrategy":"validation","validationCode":"// If a proxy rejects chunked uploads, set a Content-Length by buffering.\n// Otherwise branch on status after streaming completes.\nif (!response.isSuccessful()) { /* code + body */ return; }","typeGuard":"static boolean streamed(Response r) { return r.isSuccessful(); }","tryCatchPattern":"try {\n  // call (streamed)\n} catch (IOException e) {\n  // includes 'Unexpected code' (HTTP status) and transport errors mid-stream\n}","preventionTips":["Confirm the markdown/raw endpoint still exists.","If a proxy rejects chunked uploads, buffer the body to set Content-Length.","Add a token to avoid 403 rate limiting.","Inspect response.code() and body on failure."],"tags":["okhttp","http-status","streaming-upload","github-api","java"],"backgroundTag":null,"analyzedSha":"91a8b34c6f44bd28c421364f8edadc9f324dddd9","analyzedAt":"2026-08-10T18:39:54.316Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}