{"record":{"id":"e1fb5608da17ab9b","repo":"square/okhttp","slug":"unexpected-code-e1fb56","errorCode":null,"errorMessage":"Unexpected code ","messagePattern":"Unexpected code ","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"samples/guide/src/main/java/okhttp3/recipes/PostFile.java","lineNumber":41,"sourceCode":"import okhttp3.RequestBody;\nimport okhttp3.Response;\n\npublic final class PostFile {\n  public static final MediaType MEDIA_TYPE_MARKDOWN\n      = MediaType.get(\"text/x-markdown; charset=utf-8\");\n\n  private final OkHttpClient client = new OkHttpClient();\n\n  public void run() throws Exception {\n    File file = new File(\"README.md\");\n\n    Request request = new Request.Builder()\n        .url(\"https://api.github.com/markdown/raw\")\n        .post(RequestBody.create(file, MEDIA_TYPE_MARKDOWN))\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 PostFile().run();\n  }\n}\n","sourceCodeStart":23,"sourceCodeEnd":51,"githubUrl":"https://github.com/square/okhttp/blob/91a8b34c6f44bd28c421364f8edadc9f324dddd9/samples/guide/src/main/java/okhttp3/recipes/PostFile.java#L23-L51","documentation":"Thrown in the PostFile recipe: `if (!response.isSuccessful()) throw new IOException(\"Unexpected code \" + response)`. It POSTs the local file README.md as text/x-markdown to GitHub's markdown/raw endpoint. If the file is missing OkHttp fails earlier (different error). This guard fires when the upload reached GitHub but the server answered non-2xx.","triggerScenarios":"POST to https://api.github.com/markdown/raw with a markdown body returns 404 (endpoint moved/removed), 403 (rate limit), 400 (malformed markdown body), or 5xx. GitHub has changed/removed raw markdown rendering endpoints historically.","commonSituations":"Running from a directory with no README.md (but that fails before this line); GitHub deprecating the markdown/raw endpoint; rate limit; network/proxy rewriting the POST.","solutions":["Check response.code() and response.body().string() to read GitHub's error message.","Confirm the markdown/raw endpoint is still supported; use the markdown REST API (/markdown) if not.","Add a token header if you are being rate limited.","Verify the file exists and is readable before building the RequestBody."],"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":"// Verify the file exists before building the request; branch on status after.\nFile file = new File(\"README.md\");\nif (!file.isFile()) throw new FileNotFoundException(file.toString());\n...\nif (!response.isSuccessful()) { /* inspect code + body */ return; }","typeGuard":"static boolean uploadable(Response r) { return r.isSuccessful(); }","tryCatchPattern":"try {\n  // call\n} catch (FileNotFoundException e) {\n  // local file missing (fails before the status check)\n} catch (IOException e) {\n  // includes 'Unexpected code' (HTTP status) and transport errors\n}","preventionTips":["Check the file exists and is readable before building RequestBody.create(file, ...).","Confirm the markdown/raw endpoint is still supported by GitHub.","Add a token to avoid rate limiting.","Inspect response.code() and body on failure."],"tags":["okhttp","http-status","github-api","file-upload","java"],"backgroundTag":null,"analyzedSha":"91a8b34c6f44bd28c421364f8edadc9f324dddd9","analyzedAt":"2026-08-10T18:39:54.316Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}