square/okhttp · error · IOException
Unexpected code
Error message
Unexpected code
What it means
Thrown in the PostFile recipe: `if (!response.isSuccessful()) throw new IOException("Unexpected code " + response)`. It POSTs the local file README.md as text/x-markdown to GitHub's markdown/raw endpoint. If the file is missing OkHttp fails earlier (different error). This guard fires when the upload reached GitHub but the server answered non-2xx.
Solutions
- Check response.code() and response.body().string() to read GitHub's error message.
- Confirm the markdown/raw endpoint is still supported; use the markdown REST API (/markdown) if not.
- Add a token header if you are being rate limited.
- Verify the file exists and is readable before building the RequestBody.
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
// Verify the file exists before building the request; branch on status after.
File file = new File("README.md");
if (!file.isFile()) throw new FileNotFoundException(file.toString());
...
if (!response.isSuccessful()) { /* inspect code + body */ return; } Type guard
static boolean uploadable(Response r) { return r.isSuccessful(); } Try / catch
try {
// call
} catch (FileNotFoundException e) {
// local file missing (fails before the status check)
} catch (IOException e) {
// includes 'Unexpected code' (HTTP status) and transport errors
} Prevention
- Check the file exists and is readable before building RequestBody.create(file, ...).
- Confirm the markdown/raw endpoint is still supported by GitHub.
- Add a token to avoid rate limiting.
- Inspect response.code() and body on failure.
When it happens
Trigger: POST to https://api.github.com/markdown/raw with a markdown body returns 404 (endpoint moved/removed), 403 (rate limit), 400 (malformed markdown body), or 5xx. GitHub has changed/removed raw markdown rendering endpoints historically.
Common situations: Running from a directory with no README.md (but that fails before this line); GitHub deprecating the markdown/raw endpoint; rate limit; network/proxy rewriting the POST.
Related errors
AI-assisted analysis of square/okhttp@91a8b34c6f (2026-08-10).
Data as JSON: /api/errors/e1fb5608da17ab9b.
Report an issue: GitHub.
Appendix: source
Thrown at samples/guide/src/main/java/okhttp3/recipes/PostFile.java:41
import okhttp3.RequestBody;
import okhttp3.Response;
public final class PostFile {
public static final MediaType MEDIA_TYPE_MARKDOWN
= MediaType.get("text/x-markdown; charset=utf-8");
private final OkHttpClient client = new OkHttpClient();
public void run() throws Exception {
File file = new File("README.md");
Request request = new Request.Builder()
.url("https://api.github.com/markdown/raw")
.post(RequestBody.create(file, MEDIA_TYPE_MARKDOWN))
.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 PostFile().run();
}
}
View on GitHub (pinned to 91a8b34c6f)