{"record":{"id":"b9ddbae097a57f62","repo":"jackwener/OpenCLI","slug":"returned-invalid-json-b9ddba","errorCode":null,"errorMessage":" returned invalid JSON","messagePattern":" returned invalid JSON","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"clis/instagram/unsave.js","lineNumber":30,"sourceCode":"            positional: true,\n            help: 'Username of the post author',\n        },\n        { name: 'index', type: 'int', default: 1, help: 'Post index (1 = most recent)' },\n    ],\n    columns: ['status', 'user', 'post'],\n    pipeline: [\n        { navigate: 'https://www.instagram.com' },\n        { evaluate: `(async () => {\n  const username = \\${{ args.username | json }};\n  const idx = \\${{ args.index }} - 1;\n  if (!Number.isInteger(idx) || idx < 0) throw new Error('index must be a positive integer');\n  const headers = { 'X-IG-App-ID': '936619743392459' };\n  const opts = { credentials: 'include', headers };\n  async function readInstagramJson(response, label) {\n    try {\n      return await response.json();\n    } catch {\n      throw new Error(label + ' returned invalid JSON');\n    }\n  }\n  function getPostFromFeed(feed, label) {\n    if (!feed || typeof feed !== 'object' || !Array.isArray(feed.items)) {\n      throw new Error(label + ' returned malformed items payload');\n    }\n    if (idx >= feed.items.length) throw new Error('Post index ' + (idx + 1) + ' not found');\n    const post = feed.items[idx];\n    const pkRaw = post?.pk ?? post?.id;\n    const pk = typeof pkRaw === 'number' ? String(pkRaw) : (typeof pkRaw === 'string' ? pkRaw.trim() : '');\n    if (!/^\\\\d+$/.test(pk)) throw new Error(label + ' returned malformed post row');\n    const caption = typeof post?.caption?.text === 'string' ? post.caption.text.substring(0, 60) : '';\n    return { pk, caption };\n  }\n  function assertOkStatus(payload, label) {\n    if (!payload || typeof payload !== 'object' || payload.status !== 'ok') {\n      throw new Error(label + ' returned no success evidence');\n    }","sourceCodeStart":12,"sourceCodeEnd":48,"githubUrl":"https://github.com/jackwener/OpenCLI/blob/49907e53dc3ade5c223ff0c4c2c2785687cec4e6/clis/instagram/unsave.js#L12-L48","documentation":"One of the Instagram HTTP responses could not be parsed as JSON (response.json() threw inside readInstagramJson). The failing call is identified by the label prefix. Usually the endpoint returned HTML (login page, challenge, or error page) instead of JSON.","triggerScenarios":"feed-by-username or unsave endpoint returns non-JSON body — login redirect HTML, rate-limit page, or Cloudflare challenge — while HTTP status may still be 200.","commonSituations":"Expired session causing Instagram to return the login page with status 200; logged out of the browser profile; Instagram serving an interstitial for automated traffic.","solutions":["Log into Instagram in the browser profile the CLI drives and retry.","Retry later if rate-limited; reduce request frequency.","Check the response manually (curl with cookies) to confirm what body is returned."],"exampleFix":"// before\nconst data = await r1.json();\n// after\nlet data;\ntry { data = await r1.json(); }\ncatch { throw new Error('Instagram feed-by-username returned invalid JSON — check login session'); }","handlingStrategy":"try-catch","validationCode":"if (!document.cookie.includes('sessionid')) throw new Error('Not logged into Instagram');","typeGuard":null,"tryCatchPattern":"try {\n  await cli.unsave(user, index);\n} catch (e) {\n  if (e.message.includes('invalid JSON')) {\n    console.error('Non-JSON response — likely logged out. Re-authenticate and retry.');\n  } else throw e;\n}","preventionTips":["Ensure an active Instagram login in the browser profile","Detect login-page HTML responses early","Reduce request frequency to avoid challenge pages"],"tags":["instagram","json","network","authentication"],"backgroundTag":"invalid-json-response","analyzedSha":"49907e53dc3ade5c223ff0c4c2c2785687cec4e6","analyzedAt":"2026-08-29T08:14:47.543Z","schemaVersion":2},"datasetVersion":"2026-08-29T12:17:43.993Z"}