{"id":"0b581834c41319d2","repo":"square/okhttp","slug":"unexpected-code-0b5818","errorCode":null,"errorMessage":"Unexpected code ","messagePattern":"Unexpected code ","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"samples/guide/src/main/java/okhttp3/recipes/PostForm.java","lineNumber":38,"sourceCode":"import okhttp3.OkHttpClient;\nimport okhttp3.Request;\nimport okhttp3.RequestBody;\nimport okhttp3.Response;\n\npublic final class PostForm {\n  private final OkHttpClient client = new OkHttpClient();\n\n  public void run() throws Exception {\n    RequestBody formBody = new FormBody.Builder()\n        .add(\"search\", \"Jurassic Park\")\n        .build();\n    Request request = new Request.Builder()\n        .url(\"https://en.wikipedia.org/w/index.php\")\n        .post(formBody)\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 PostForm().run();\n  }\n}\n","sourceCodeStart":20,"sourceCodeEnd":48,"githubUrl":"https://github.com/square/okhttp/blob/4fc083138014aba3d0078f5c26d1ce84815fa984/samples/guide/src/main/java/okhttp3/recipes/PostForm.java#L20-L48","documentation":"Recipe-level guard after POSTing a URL-encoded form (search=Jurassic Park) to Wikipedia's index endpoint. If the response is non-2xx, java.io.IOException(\"Unexpected code \" + response) is thrown. Wikipedia's Special:Search via /w/index.php typically returns 200 with HTML even for no results, so reaching this guard usually means the endpoint shape changed or the request was blocked.","triggerScenarios":"POST https://en.wikipedia.org/w/index.php with form body. Fires on 405 (Wikipedia may reject POST to that path for search — search is normally GET), 415, or 403 when Wikipedia/Apache blocks the request (e.g. missing/blank User-Agent, looks like a bot).","commonSituations":"Wikipedia enforces a strict User-Agent policy and rejects empty/default UAs with 403; copying the recipe without adding a descriptive User-Agent; expecting JSON but Wikipedia returns HTML.","solutions":["Add a descriptive User-Agent header per Wikimedia policy (e.g. 'MyBot/1.0 (contact@example.com)').","If you only need to search, use GET with ?title=Special:Search&search=... or the Action API (/w/api.php) instead of POSTing the form.","Inspect response.code() and the response body (often HTML with an explanatory error)."],"exampleFix":"// before\nRequest request = new Request.Builder()\n    .url(\"https://en.wikipedia.org/w/index.php\")\n    .post(formBody)\n    .build();\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://en.wikipedia.org/w/index.php\")\n    .header(\"User-Agent\", \"MyBot/1.0 (contact@example.com)\")\n    .post(formBody)\n    .build();\ntry (Response response = client.newCall(request).execute()) {\n  if (!response.isSuccessful()) {\n    throw new IOException(\"Wikipedia returned \" + response.code()\n        + \": \" + response.body().string().substring(0, 200));\n  }\n  ...\n}","handlingStrategy":"validation","validationCode":"// Wikimedia requires a descriptive User-Agent; add one and prefer the Action API\nRequest request = new Request.Builder()\n    .url(\"https://en.wikipedia.org/w/api.php?action=query&list=search&srsearch=Jurassic+Park&format=json\")\n    .header(\"User-Agent\", \"MyBot/1.0 (contact@example.com)\")\n    .build();","typeGuard":null,"tryCatchPattern":"try (Response response = client.newCall(request).execute()) {\n  if (!response.isSuccessful()) {\n    if (response.code() == 403) throw new BlockedException(\"Likely User-Agent policy block\");\n    if (response.code() == 405) throw new MethodNotAllowedException(\"Use GET /w/api.php instead of POST /w/index.php\");\n    throw new HttpException(response.code(), response.message());\n  }\n}","preventionTips":["Always send a descriptive User-Agent to Wikimedia endpoints.","Prefer the Action API (/w/api.php) for structured search rather than POSTing the form.","Handle 403 as a likely UA block rather than a transient error.","Do not assume a 200 body is JSON — Wikipedia returns HTML for /w/index.php."],"tags":["okhttp","http-status","form-post","user-agent","wikipedia","java"],"analyzedSha":"4fc083138014aba3d0078f5c26d1ce84815fa984","analyzedAt":"2026-08-04T19:09:04.639Z","schemaVersion":2}