{"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/4fc083138014aba3d0078f5c26d1ce84815fa984/samples/guide/src/main/java/okhttp3/recipes/PostFile.java#L23-L51","documentation":"Recipe-level guard after POSTing a local file (README.md) as text/x-markdown to GitHub's markdown rendering endpoint. If the response is non-2xx, java.io.IOException(\"Unexpected code \" + response) is thrown. Note: if README.md does not exist, RequestBody.create(file, ...) throws FileNotFoundException earlier on the executing thread, not this line.","triggerScenarios":"POST https://api.github.com/markdown/raw with body from new File(\"README.md\"). Reaches this line when the file was read and sent but GitHub returned non-2xx: 403 (rate limit), 404/410 (endpoint moved), 415 (unsupported media type — e.g. content-type wrong), or 400 (malformed markdown payload).","commonSituations":"Running the recipe from a directory with no README.md (→ earlier FileNotFoundException, often confused with this); GitHub rate-limiting raw markdown POSTs; changing the media type to something GitHub rejects; CWD-relative file resolution meaning the file is read from a different place than expected.","solutions":["Resolve the File with an absolute path or load it as a classpath resource to avoid CWD confusion.","Inspect response.code() — 415 means the media type is wrong; 403 means rate limit.","Confirm README.md exists and is readable before building the RequestBody.","Authenticate POSTs to GitHub to avoid the 60/hr unauthenticated cap."],"exampleFix":"// before\nFile file = new File(\"README.md\");\nRequest request = new Request.Builder()\n    .url(\"https://api.github.com/markdown/raw\")\n    .post(RequestBody.create(file, MEDIA_TYPE_MARKDOWN))\n    .build();\ntry (Response response = client.newCall(request).execute()) {\n  if (!response.isSuccessful()) throw new IOException(\"Unexpected code \" + response);\n  ...\n}\n\n// after\nFile file = new File(System.getProperty(\"user.dir\"), \"README.md\");\nif (!file.isFile()) throw new IOException(\"Missing input file: \" + file.getAbsolutePath());\nRequest request = new Request.Builder()\n    .url(\"https://api.github.com/markdown/raw\")\n    .post(RequestBody.create(file, MEDIA_TYPE_MARKDOWN))\n    .build();\ntry (Response response = client.newCall(request).execute()) {\n  if (!response.isSuccessful()) {\n    throw new IOException(\"GitHub markdown API returned \" + response.code());\n  }\n  ...\n}","handlingStrategy":"validation","validationCode":"// Validate the file before building the request body\nFile file = new File(System.getProperty(\"user.dir\"), \"README.md\");\nif (!file.isFile()) throw new FileNotFoundException(file.getAbsolutePath());\nif (file.length() > MAX_BYTES) throw new IllegalArgumentException(\"file too large: \" + file.length());\nRequestBody body = RequestBody.create(file, MediaType.get(\"text/x-markdown; charset=utf-8\"));","typeGuard":null,"tryCatchPattern":"try {\n  Response response = client.newCall(request).execute();\n  if (!response.isSuccessful()) {\n    if (response.code() == 415) throw new UnsupportedMediaTypeException(\"GitHub expects text/x-markdown\");\n    if (response.code() == 403) throw new RateLimitedException(response.header(\"X-RateLimit-Reset\"));\n    throw new HttpException(response.code(), response.message());\n  }\n} catch (FileNotFoundException e) {\n  // input file missing — surfaces from RequestBody.create / execute, distinct from HTTP errors\n  throw new InputMissingException(\"README.md not found at CWD\", e);\n}","preventionTips":["Resolve the input file with an absolute path or a classpath resource.","Authenticate GitHub POSTs to avoid the unauthenticated cap.","Set the MediaType explicitly to text/x-markdown; charset=utf-8.","Distinguish FileNotFoundException (input) from HTTP 4xx (server)."],"tags":["okhttp","http-status","file-upload","github-api","post","java"],"analyzedSha":"4fc083138014aba3d0078f5c26d1ce84815fa984","analyzedAt":"2026-08-04T19:09:04.639Z","schemaVersion":2}