{"id":"ff1f21b7742d3696","repo":"square/okhttp","slug":"unexpected-code-ff1f21","errorCode":null,"errorMessage":"Unexpected code ","messagePattern":"Unexpected code ","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"samples/guide/src/main/java/okhttp3/recipes/PostString.java","lineNumber":46,"sourceCode":"\n  private final OkHttpClient client = new OkHttpClient();\n\n  public void run() throws Exception {\n    String postBody = \"\"\n        + \"Releases\\n\"\n        + \"--------\\n\"\n        + \"\\n\"\n        + \" * _1.0_ May 6, 2013\\n\"\n        + \" * _1.1_ June 15, 2013\\n\"\n        + \" * _1.2_ August 11, 2013\\n\";\n\n    Request request = new Request.Builder()\n        .url(\"https://api.github.com/markdown/raw\")\n        .post(RequestBody.create(postBody, 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 PostString().run();\n  }\n}\n","sourceCodeStart":28,"sourceCodeEnd":56,"githubUrl":"https://github.com/square/okhttp/blob/4fc083138014aba3d0078f5c26d1ce84815fa984/samples/guide/src/main/java/okhttp3/recipes/PostString.java#L28-L56","documentation":"Recipe-level guard after POSTing a fixed String (markdown releases list) as text/x-markdown to GitHub's markdown/raw endpoint. If the response is non-2xx, java.io.IOException(\"Unexpected code \" + response) is thrown. This is the simplest of the POST recipes — no file IO, no streaming — so reaching this line means the request was sent and the server replied with an error status.","triggerScenarios":"POST https://api.github.com/markdown/raw with RequestBody.create(postBody, MEDIA_TYPE_MARKDOWN). Fires on 403 (unauthenticated rate limit), 415 (media type wrong), 404/410 (endpoint gone), 400 (body not valid markdown the endpoint expects).","commonSituations":"GitHub unauthenticated rate limit (60/hr) when iterating; changing MEDIA_TYPE to a value GitHub rejects; endpoint behaviour changed; missing User-Agent.","solutions":["Check response.code() and X-RateLimit-* headers.","Authenticate the request with a token.","Ensure MEDIA_TYPE is text/x-markdown; charset=utf-8.","Add a User-Agent header."],"exampleFix":"// before\ntry (Response response = client.newCall(request).execute()) {\n  if (!response.isSuccessful()) throw new IOException(\"Unexpected code \" + response);\n  ...\n}\n\n// after\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(RequestBody.create(postBody, MEDIA_TYPE_MARKDOWN))\n    .build();\ntry (Response response = client.newCall(request).execute()) {\n  if (!response.isSuccessful()) {\n    throw new IOException(\"GitHub \" + response.code()\n        + \" remaining=\" + response.header(\"X-RateLimit-Remaining\"));\n  }\n  ...\n}","handlingStrategy":"validation","validationCode":"// Authenticate, set UA, and verify media type before posting\nString token = System.getenv(\"GITHUB_TOKEN\");\nMediaType mediaType = MediaType.get(\"text/x-markdown; charset=utf-8\");\nRequest request = new Request.Builder()\n    .url(\"https://api.github.com/markdown/raw\")\n    .header(\"Authorization\", token != null ? \"Bearer \" + token : null)\n    .header(\"User-Agent\", \"MyApp/1.0\")\n    .post(RequestBody.create(postBody, 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(\"Expected 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}","preventionTips":["Authenticate GitHub POSTs with a Bearer token to avoid the 60/hr cap.","Always set the media type to text/x-markdown; charset=utf-8.","Send a User-Agent header.","Branch on response.code() rather than a single throw."],"tags":["okhttp","http-status","post","github-api","rate-limit","java"],"analyzedSha":"4fc083138014aba3d0078f5c26d1ce84815fa984","analyzedAt":"2026-08-04T19:09:04.639Z","schemaVersion":2}