{"record":{"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/91a8b34c6f44bd28c421364f8edadc9f324dddd9/samples/guide/src/main/java/okhttp3/recipes/PostForm.java#L20-L48","documentation":"Thrown in the PostForm recipe: `if (!response.isSuccessful()) throw new IOException(\"Unexpected code \" + response)`. It POSTs an application/x-www-form-urlencoded body (search=Jurassic Park) to Wikipedia's index.php. Wikipedia returns non-2xx (often a 200 with HTML, but 400/405/302 are possible) and the guard trips.","triggerScenarios":"POST to https://en.wikipedia.org/w/index.php returns 302 (Wikipedia commonly redirects form POSTs), 405 (method not allowed for that path), 400, or 5xx. Wikipedia's search endpoint may expect a different form field set or a GET.","commonSituations":"Wikipedia changing its form handling / anti-abuse rules; missing required hidden form fields; running behind a filter that blocks POST; expecting JSON but getting HTML (still 200, so usually does not throw).","solutions":["Inspect response.code() and follow redirects explicitly if needed.","Match Wikipedia's actual search form fields (some endpoints need 'title'/'limit').","Use the MediaWiki Action API (api.php) for structured search instead of scraping index.php.","Branch on code rather than throwing so the HTML body can be inspected."],"exampleFix":"// before\nif (!response.isSuccessful()) throw new IOException(\"Unexpected code \" + response);\n\n// after\nif (!response.isSuccessful()) {\n  System.err.println(\"wiki POST returned \" + response.code());\n  return;\n}","handlingStrategy":"validation","validationCode":"// Match Wikipedia's expected form fields and branch on status/redirects.\nRequestBody form = new FormBody.Builder().add(\"search\", \"Jurassic Park\").add(\"ns0\", \"1\").build();\n...\nif (!response.isSuccessful()) { /* code + body */ return; }","typeGuard":"static boolean isHtmlResult(Response r) { return r.isSuccessful() && \"text/html\".equals(r.body().contentType().subtype()); }","tryCatchPattern":"try {\n  // call\n} catch (IOException e) {\n  // includes 'Unexpected code' (HTTP status) and transport errors\n}","preventionTips":["Use the MediaWiki Action API (api.php) for structured data instead of scraping index.php.","Match Wikipedia's actual form fields; some endpoints need hidden fields.","Inspect response.code(); Wikipedia commonly 302-redirects form POSTs.","Branch on code rather than throwing."],"tags":["okhttp","http-status","form-post","java"],"backgroundTag":null,"analyzedSha":"91a8b34c6f44bd28c421364f8edadc9f324dddd9","analyzedAt":"2026-08-10T18:39:54.316Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}