{"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/4fc083138014aba3d0078f5c26d1ce84815fa984/samples/guide/src/main/java/okhttp3/recipes/PostStreaming.java#L43-L71","documentation":"Recipe-level guard after POSTing a streaming RequestBody (an anonymous subclass that writes generated markdown prime factorisations to the BufferedSink) to GitHub's markdown/raw endpoint. If the response is non-2xx, java.io.IOException(\"Unexpected code \" + response) is thrown. The body is produced on-demand during writeTo(), so a writeTo() IOException (e.g. the connection is closed mid-upload) surfaces from execute(), not this line.","triggerScenarios":"POST https://api.github.com/markdown/raw with a chunked streaming body. Reaches this line when the body was sent successfully but GitHub returned 4xx/5xx: 403 (rate limit), 415 (media type mismatch — must be text/x-markdown), 404/410 (endpoint retired), 400 (malformed body).","commonSituations":"Wrong content-type in contentType() (must match the .header or the endpoint's expectation); GitHub rate limit on the unauthenticated markdown API; a writeTo() bug producing an invalid UTF-8 sequence (would throw earlier).","solutions":["Verify contentType() returns exactly text/x-markdown; charset=utf-8.","Inspect response.code() and rate-limit headers.","Authenticate to GitHub to lift the cap.","Write a small, deterministic test payload first to isolate content vs. transport issues."],"exampleFix":"// before\ntry (Response response = client.newCall(request).execute()) {\n  if (!response.isSuccessful()) throw new IOException(\"Unexpected code \" + response);\n  ...\n}\n\n// after\ntry (Response response = client.newCall(request).execute()) {\n  if (!response.isSuccessful()) {\n    throw new IOException(\"GitHub markdown/raw \" + response.code()\n        + \" (contentType=\" + MEDIA_TYPE_MARKDOWN + \")\");\n  }\n  ...\n}","handlingStrategy":"validation","validationCode":"// Ensure the streaming body's content type matches what the endpoint expects\nMediaType mediaType = MediaType.get(\"text/x-markdown; charset=utf-8\");\n// keep contentType() returning exactly this; authenticate the request:\nRequest request = new Request.Builder()\n    .url(\"https://api.github.com/markdown/raw\")\n    .header(\"Authorization\", \"Bearer \" + token)\n    .header(\"User-Agent\", \"MyApp/1.0\")\n    .post(streamingBody)  // contentType() == mediaType\n    .build();","typeGuard":null,"tryCatchPattern":"try (Response response = client.newCall(request).execute()) {\n  if (!response.isSuccessful()) {\n    if (response.code() == 415) throw new UnsupportedMediaTypeException(\"Use text/x-markdown; charset=utf-8\");\n    if (response.code() == 403) throw new RateLimitedException(response.header(\"X-RateLimit-Reset\"));\n    throw new HttpException(response.code(), response.message());\n  }\n}","preventionTips":["Make the anonymous RequestBody's contentType() return exactly the expected media type.","Authenticate GitHub POSTs and respect rate limits.","Keep writeTo() deterministic and UTF-8 clean to avoid pre-throw IOExceptions.","Use a small fixed payload in tests to isolate content vs. transport errors."],"tags":["okhttp","http-status","streaming-upload","post","github-api","java"],"analyzedSha":"4fc083138014aba3d0078f5c26d1ce84815fa984","analyzedAt":"2026-08-04T19:09:04.639Z","schemaVersion":2}