jackwener/OpenCLI · error · Error
Failed to fetch following: HTTP ' + r2.status
Error message
Failed to fetch following: HTTP ' + r2.status
What it means
In the following command's pagination loop, each page is fetched from https://www.instagram.com/api/v1/friendships/<userId>/following/?count=50(&max_id=...). A non-ok HTTP status on any page throws this error with the status code. Because it's inside the while(results.length < limit) loop, the error can fire mid-pagination even if earlier pages succeeded.
Source
Thrown at clis/instagram/following.js:36
const limit = \${{ args.limit }};
if (!Number.isInteger(limit) || limit < 1) throw new Error('limit must be a positive integer');
const headers = { 'X-IG-App-ID': '936619743392459' };
const opts = { credentials: 'include', headers };
${buildResolveInstagramUserIdJs()}
const PAGE_SIZE = 50;
const results = [];
const seen = new Set();
const seenCursors = new Set();
let maxId = undefined;
const baseUrl = 'https://www.instagram.com/api/v1/friendships/' + userId + '/following/';
while (results.length < limit) {
const params = new URLSearchParams({ count: String(PAGE_SIZE) });
if (maxId) params.set('max_id', maxId);
const r2 = await fetch(baseUrl + '?' + params.toString(), opts);
if (!r2.ok) throw new Error('Failed to fetch following: HTTP ' + r2.status);
const d2 = await r2.json();
if (!d2 || typeof d2 !== 'object' || !Array.isArray(d2.users)) {
throw new Error('Instagram following returned malformed users payload');
}
const users = d2.users;
const sizeBefore = results.length;
for (const u of users) {
if (!u || typeof u !== 'object') {
throw new Error('Instagram following returned malformed user row');
}
const pkRaw = u.pk ?? u.pk_id ?? u.id;
const pk = typeof pkRaw === 'number' ? String(pkRaw) : (typeof pkRaw === 'string' ? pkRaw.trim() : '');
const usernameValue = typeof u.username === 'string' ? u.username.trim() : '';
if (!/^\\d+$/.test(pk) || !usernameValue) {
throw new Error('Instagram following returned malformed user row');
}
if (!pk || seen.has(pk)) continue;
seen.add(pk);View on GitHub (pinned to 49907e53dc)
Solutions
- Re-authenticate in the browser session (401/403).
- Lower --limit to fetch fewer pages and add delays between requests to avoid 429.
- Retry with exponential backoff; capture the HTTP status from the message to decide.
- Verify the username resolves to a valid, accessible userId (404/403).
Example fix
// before
catch (e) { console.error(e.message); }
// after
catch (e) {
const m = /HTTP (\d+)/.exec(e.message);
if (m && +m[1] === 429) { await sleep(60000); return retryWithLowerLimit(); }
if (m && (+m[1] === 401 || +m[1] === 403)) return promptRelogin();
throw e;
} Defensive patterns
Strategy: retry
Validate before calling
// Pre-flight before paginating:
const authed = document.cookie.includes('ds_user_id');
const limitOk = Number.isInteger(limit) && limit >= 1;
if (!authed || !limitOk) throw new Error('Preconditions failed: session or limit invalid'); Try / catch
async function fetchFollowingPages(userId, limit) {
let maxId = null;
for (let attempt = 0; attempt < 3; attempt++) {
try {
const page = await fetchPage(userId, maxId);
maxId = page.next_max_id;
if (!maxId) break;
} catch (e) {
const code = +(/HTTP (\d+)/.exec(String(e.message))?.[1] ?? 0);
if (code !== 429 && code < 500) throw e;
await new Promise(r => setTimeout(r, 2 ** attempt * 5000));
}
}
} Prevention
- Add delays between paginated requests; large limits on big accounts trigger 429 quickly.
- Keep the session alive across the whole pagination run (re-login on 401).
- Parse the HTTP status out of the error message to choose retry vs abort.
- Cap limit to what you need — each extra page is another rate-limited request.
When it happens
Trigger: 401/403 (expired cookies) on page 1 or after; 429 rate limit triggered by repeated page fetches; 404 from bad userId; 5xx from Instagram; anti-bot challenge blocking subsequent paginated requests.
Common situations: Large limits causing many rapid paginated requests (429 after a few pages); session expiring between pages; following list of a private/blocked account returning 403; Instagram throttling max_id cursor requests.
Understand the failure class
Background: "API error: {status}" and "HTTP 401/403/404/429/5xx" errors: non-2xx HTTP responses explained — this error's family across 27 libraries.
Related errors
- Failed to fetch followers: HTTP ' + r2.status
- archive snapshots failed: HTTP ${resp.status}
- Instagram private publish ${stage} failed: ${response.status
- ${label} failed: HTTP ${response.status}
- ${label} failed: HTTP ${response.status}
AI-assisted analysis of jackwener/OpenCLI@49907e53dc (2026-08-29).
Data as JSON: /api/errors/07a5b6d19e776051.
Report an issue: GitHub.