{"record":{"id":"1cda414ffab12371","repo":"jackwener/OpenCLI","slug":"failed-to-comment-http-r2-status","errorCode":null,"errorMessage":"Failed to comment: HTTP ' + r2.status","messagePattern":"Failed to comment: HTTP ' \\+ r2\\.status","errorType":"http","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"clis/instagram/comment.js","lineNumber":63,"sourceCode":"  }\n  function assertOkStatus(payload, label) {\n    if (!payload || typeof payload !== 'object' || payload.status !== 'ok') {\n      throw new Error(label + ' returned no success evidence');\n    }\n  }\n\n  // web_profile_info answers HTTP 400 for business accounts; feed-by-username needs no user id. See #2234.\n  const r1 = await fetch('https://www.instagram.com/api/v1/feed/user/' + encodeURIComponent(username) + '/username/?count=' + (idx + 1), opts);\n  if (!r1.ok) throw new Error(r1.status === 404 ? 'User not found: ' + username : 'HTTP ' + r1.status + ' - make sure you are logged in to Instagram');\n  const { pk } = getPostFromFeed(await readInstagramJson(r1, 'Instagram feed-by-username'), 'Instagram feed-by-username');\n\n  const csrf = document.cookie.match(/csrftoken=([^;]+)/)?.[1] || '';\n  const r2 = await fetch('https://www.instagram.com/api/v1/web/comments/' + pk + '/add/', {\n    method: 'POST', credentials: 'include',\n    headers: { ...headers, 'X-CSRFToken': csrf, 'Content-Type': 'application/x-www-form-urlencoded' },\n    body: 'comment_text=' + encodeURIComponent(commentText),\n  });\n  if (!r2.ok) throw new Error('Failed to comment: HTTP ' + r2.status);\n  assertOkStatus(await readInstagramJson(r2, 'Instagram comment'), 'Instagram comment');\n  return [{ status: 'Commented', user: username, text: commentText }];\n})()\n` },\n    ],\n});\n","sourceCodeStart":45,"sourceCodeEnd":70,"githubUrl":"https://github.com/jackwener/OpenCLI/blob/49907e53dc3ade5c223ff0c4c2c2785687cec4e6/clis/instagram/comment.js#L45-L70","documentation":"The POST to /api/v1/web/comments/{pk}/add/ returned a non-ok HTTP status, so the CLI throws 'Failed to comment: HTTP <status>'. The comment was not accepted at the transport level (auth, permissions, or rate limiting), distinct from in-body status failures.","triggerScenarios":"Missing/invalid CSRF token (403); session expired (401/403); comments disabled on the target post (often 403); spam/rate limiting (429 or 400); post deleted between fetch and comment (404).","commonSituations":"csrftoken cookie absent because login state is broken; automation posting identical comments rapidly; commenting on very old posts with comments turned off; IG hardening web endpoints requiring extra headers.","solutions":["Check the csrftoken cookie is present and passed as X-CSRFToken; re-login if missing","Reduce comment frequency and vary text to avoid 400/429 spam throttling","Confirm the target post allows comments","Re-send with full browser-like headers (X-IG-App-ID, X-Requested-With, Referer) if IG tightened checks"],"exampleFix":"// before\nif (!r2.ok) throw new Error('Failed to comment: HTTP ' + r2.status);\n// after\nif (!r2.ok) {\n  const body = await r2.text().catch(() => '');\n  if (r2.status === 403 && !csrf) throw new Error('Comment failed: missing csrftoken - re-login');\n  throw new Error('Failed to comment: HTTP ' + r2.status + (body ? ' - ' + body.slice(0, 200) : ''));\n}","handlingStrategy":"try-catch","validationCode":"const csrf = document.cookie.match(/csrftoken=([^;]+)/)?.[1] || '';\nif (!csrf) throw new Error('No csrftoken cookie - re-login before commenting');","typeGuard":"function canComment(csrf, cookies) {\n  return typeof csrf === 'string' && csrf.length > 0 && cookies.includes('sessionid');\n}","tryCatchPattern":"try {\n  await runCommentPipeline(args);\n} catch (e) {\n  const m = /Failed to comment: HTTP (\\d+)/.exec(e.message);\n  if (m) {\n    if (m[1] === '403') console.error('CSRF/auth problem - re-login');\n    else if (m[1] === '429' || m[1] === '400') console.error('Throttled - back off and vary comment text');\n  } else throw e;\n}","preventionTips":["Always send X-CSRFToken from a valid session","Add jitter/delays between comments","Deduplicate comment texts","Send browser-like headers (X-IG-App-ID, Referer)"],"tags":["instagram","http-status","csrf","comment"],"backgroundTag":"http-403-forbidden","analyzedSha":"49907e53dc3ade5c223ff0c4c2c2785687cec4e6","analyzedAt":"2026-08-29T08:14:47.543Z","schemaVersion":2},"datasetVersion":"2026-08-29T12:17:43.993Z"}