square/okhttp · error · IOException
Unexpected code
Error message
Unexpected code
What it means
Thrown in the PostStreaming recipe: `if (!response.isSuccessful()) throw new IOException("Unexpected code " + response)`. It streams generated markdown (a prime-factorization table) to GitHub's markdown/raw endpoint via a custom RequestBody whose writeTo writes to the sink on demand. The guard fires when GitHub answers non-2xx after the stream completes.
Solutions
- Check response.code() and the body for GitHub's error message.
- If a proxy rejects chunked uploads, buffer the body and set a Content-Length (use RequestBody.create with bytes).
- Add a token header to avoid 403 rate limiting.
- Confirm the markdown/raw endpoint still exists; switch to /markdown if needed.
Example fix
// before
if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
// after
if (!response.isSuccessful()) {
throw new IOException("markdown/raw HTTP " + response.code() + ": " + response.body().string());
} Defensive patterns
Strategy: validation
Validate before calling
// If a proxy rejects chunked uploads, set a Content-Length by buffering.
// Otherwise branch on status after streaming completes.
if (!response.isSuccessful()) { /* code + body */ return; } Type guard
static boolean streamed(Response r) { return r.isSuccessful(); } Try / catch
try {
// call (streamed)
} catch (IOException e) {
// includes 'Unexpected code' (HTTP status) and transport errors mid-stream
} Prevention
- Confirm the markdown/raw endpoint still exists.
- If a proxy rejects chunked uploads, buffer the body to set Content-Length.
- Add a token to avoid 403 rate limiting.
- Inspect response.code() and body on failure.
When it happens
Trigger: POST https://api.github.com/markdown/raw with a streamed markdown body returns 404 (endpoint gone), 403 (rate limit), 400 (body too large or malformed), or 5xx. Because the body is streamed without Content-Length, some servers/proxies reject it; GitHub historically supports chunked markdown/raw but may change.
Common situations: GitHub deprecating markdown/raw; rate limit; a proxy that requires Content-Length rejecting the chunked POST; generating an unexpectedly huge body (primes up to 997 is small, but the pattern scales).
Related errors
AI-assisted analysis of square/okhttp@91a8b34c6f (2026-08-10).
Data as JSON: /api/errors/5027c105b57a619a.
Report an issue: GitHub.
Appendix: source
Thrown at samples/guide/src/main/java/okhttp3/recipes/PostStreaming.java:61
}
}
private String factor(int n) {
for (int i = 2; i < n; i++) {
int x = n / i;
if (x * i == n) return factor(x) + " × " + i;
}
return Integer.toString(n);
}
};
Request request = new Request.Builder()
.url("https://api.github.com/markdown/raw")
.post(requestBody)
.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 PostStreaming().run();
}
}
View on GitHub (pinned to 91a8b34c6f)