square/okhttp · error · IOException
Unexpected code
Error message
Unexpected code
What it means
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.
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.
Example fix
// before
if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
// after
if (!response.isSuccessful()) {
System.err.println("wiki POST returned " + response.code());
return;
} Defensive patterns
Strategy: validation
Validate before calling
// Match Wikipedia's expected form fields and branch on status/redirects.
RequestBody form = new FormBody.Builder().add("search", "Jurassic Park").add("ns0", "1").build();
...
if (!response.isSuccessful()) { /* code + body */ return; } Type guard
static boolean isHtmlResult(Response r) { return r.isSuccessful() && "text/html".equals(r.body().contentType().subtype()); } Try / catch
try {
// call
} catch (IOException e) {
// includes 'Unexpected code' (HTTP status) and transport errors
} Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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).
Related errors
AI-assisted analysis of square/okhttp@91a8b34c6f (2026-08-10).
Data as JSON: /api/errors/0b581834c41319d2.
Report an issue: GitHub.
Appendix: source
Thrown at samples/guide/src/main/java/okhttp3/recipes/PostForm.java:38
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
public final class PostForm {
private final OkHttpClient client = new OkHttpClient();
public void run() throws Exception {
RequestBody formBody = new FormBody.Builder()
.add("search", "Jurassic Park")
.build();
Request request = new Request.Builder()
.url("https://en.wikipedia.org/w/index.php")
.post(formBody)
.build();
try (Response response = client.newCall(request).execute()) {
if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
System.out.println(response.body().string());
}
}
public static void main(String... args) throws Exception {
new PostForm().run();
}
}
View on GitHub (pinned to 91a8b34c6f)