cogent

raw JSON →
1.0.1 verified Sat Apr 25 auth: no javascript maintenance

Cogent is a simple HTTP request library built for Node.js and generators using co. It supports redirect resolution, timeout, automatic gunzip decompression, and first-class JSON parsing. The response can be buffered, saved to a file, or returned as parsed JSON. Options include retries, timeouts, and customizable request properties. Version 1.0.1 is stable but the package is part of the cojs ecosystem and has seen no recent updates. It relies on generators and the co library, making it less suitable for modern async/await workflows without additional wrapping.

error TypeError: request(...) is not a function
cause Attempting to call the generator without using yield* or co.
fix
Use yield* request(uri) inside a generator, or wrap with co.
error ReferenceError: yield is not defined
cause Using yield outside of a generator function.
fix
Ensure the containing function is a generator (function*).
deprecated Generator-based APIs are outdated; prefer async/await with modern HTTP libraries like node-fetch or axios.
fix Use async/await with a library that returns promises (e.g., node-fetch).
gotcha When using 'cogent' without co, the generator must be yielded manually.
fix Wrap with co: const wrapped = co(require('cogent'));
gotcha The 'json' option only populates res.body on a 200 status code; non-200 responses will not have a parsed body.
fix Check res.statusCode before accessing res.body.
npm install cogent
yarn add cogent
pnpm add cogent

Demonstrates fetching a JSON endpoint using a generator function, parsing JSON response, and logging the body.

const co = require('co');
const request = require('cogent');

co(function* () {
  const res = yield* request('https://api.github.com/repos/visionmedia/express/package.json', { json: true });
  console.log(res.body);
}).catch(err => console.error(err));