jhy/jsoup · error · IllegalArgumentException
You must execute the request before getting a response.
Error message
You must execute the request before getting a response.
What it means
HttpConnection.Response is only populated after Connection.execute() (or get()/post()) completes. Calling response() on the Connection before executing throws this IllegalArgumentException because the internal `res` field is still null. It guards against reading a response that does not exist yet.
Solutions
- Call Connection.execute() (or get()/post()) before response()
- Use conn.execute().response() chained form so ordering is enforced
- Prefer Jsoup.connect(url).execute() and hold the returned Response directly
Example fix
// before Connection conn = Jsoup.connect(url); Document doc = conn.response().parse(); // after Connection.Response res = Jsoup.connect(url).execute(); Document doc = res.parse();
Defensive patterns
Strategy: try-catch
Validate before calling
if (connection instanceof HttpConnection) { /* prefer tracking your own executed flag */ } Try / catch
try { return conn.response(); } catch (IllegalArgumentException e) { return conn.execute(); } Prevention
- Always call execute() (or get()/post()) before reading response()
- Keep a boolean 'executed' flag in wrappers around HttpConnection
- Prefer the single-call forms Jsoup.connect(url).execute() / .get()
When it happens
Trigger: Calling conn.response() after Jsoup.connect(url) but before conn.execute(); calling response() on a freshly built Connection; chaining that reads response before execute().
Common situations: Refactoring from one-shot Jsoup.connect(...).get() to multi-step execute()/response() flows; forgetting that response() is a getter on Connection, not an executor; reading response in a finally block before execute succeeded.
Understand the failure class
Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.
Related errors
- URL not set. Make sure to call #url(...) before executing…
- Cannot follow redirect with a streamed request body…
- HTTP error fetching URL
- Unhandled content type. Must be a text or XML media type
- Too many redirects occurred trying to load URL
AI-assisted analysis of jhy/jsoup@9851ac5d9c (2026-09-08).
Data as JSON: /api/errors/ac0886010c6d3a4b.
Report an issue: GitHub.
Appendix: source
Thrown at src/main/java/org/jsoup/helper/HttpConnection.java:383
res = Response.execute(req);
return res;
}
@Override
public Connection.Request request() {
return req;
}
@Override
public Connection request(Connection.Request request) {
req = (HttpConnection.Request) request; // will throw a class-cast exception if the user has extended some but not all of Connection; that's desired
return this;
}
@Override
public Connection.Response response() {
if (res == null) {
throw new IllegalArgumentException("You must execute the request before getting a response.");
}
return res;
}
@Override
public Connection response(Connection.Response response) {
res = response;
return this;
}
@Override
public Connection postDataCharset(String charset) {
req.postDataCharset(charset);
return this;
}
@Override public Connection auth(@Nullable RequestAuthenticator authenticator) {
req.auth(authenticator);View on GitHub (pinned to 9851ac5d9c)