square/okhttp · error · IOException
Unexpected code
Error message
Unexpected code
What it means
Recipe-level guard after POSTing a local file (README.md) as text/x-markdown to GitHub's markdown rendering endpoint. If the response is non-2xx, java.io.IOException("Unexpected code " + response) is thrown. Note: if README.md does not exist, RequestBody.create(file, ...) throws FileNotFoundException earlier on the executing thread, not this line.
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 4fc0831380)
Solutions
- Resolve the File with an absolute path or load it as a classpath resource to avoid CWD confusion.
- Inspect response.code() — 415 means the media type is wrong; 403 means rate limit.
- Confirm README.md exists and is readable before building the RequestBody.
- Authenticate POSTs to GitHub to avoid the 60/hr unauthenticated cap.
Example fix
// before
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);
...
}
// after
File file = new File(System.getProperty("user.dir"), "README.md");
if (!file.isFile()) throw new IOException("Missing input file: " + file.getAbsolutePath());
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("GitHub markdown API returned " + response.code());
}
...
} Defensive patterns
Strategy: validation
Validate before calling
// Validate the file before building the request body
File file = new File(System.getProperty("user.dir"), "README.md");
if (!file.isFile()) throw new FileNotFoundException(file.getAbsolutePath());
if (file.length() > MAX_BYTES) throw new IllegalArgumentException("file too large: " + file.length());
RequestBody body = RequestBody.create(file, MediaType.get("text/x-markdown; charset=utf-8")); Try / catch
try {
Response response = client.newCall(request).execute();
if (!response.isSuccessful()) {
if (response.code() == 415) throw new UnsupportedMediaTypeException("GitHub expects text/x-markdown");
if (response.code() == 403) throw new RateLimitedException(response.header("X-RateLimit-Reset"));
throw new HttpException(response.code(), response.message());
}
} catch (FileNotFoundException e) {
// input file missing — surfaces from RequestBody.create / execute, distinct from HTTP errors
throw new InputMissingException("README.md not found at CWD", e);
} Prevention
- Resolve the input file with an absolute path or a classpath resource.
- Authenticate GitHub POSTs to avoid the unauthenticated cap.
- Set the MediaType explicitly to text/x-markdown; charset=utf-8.
- Distinguish FileNotFoundException (input) from HTTP 4xx (server).
When it happens
Trigger: POST https://api.github.com/markdown/raw with body from new File("README.md"). Reaches this line when the file was read and sent but GitHub returned non-2xx: 403 (rate limit), 404/410 (endpoint moved), 415 (unsupported media type — e.g. content-type wrong), or 400 (malformed markdown payload).
Common situations: Running the recipe from a directory with no README.md (→ earlier FileNotFoundException, often confused with this); GitHub rate-limiting raw markdown POSTs; changing the media type to something GitHub rejects; CWD-relative file resolution meaning the file is read from a different place than expected.
Related errors
AI-assisted analysis of square/okhttp@4fc0831380 (2026-08-04).
Data as JSON: /data/errors/e1fb5608da17ab9b.json.
Report an issue: GitHub.