Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save eyalcohen4/488cc46b5a63751ac1fe668d00d22c2b to your computer and use it in GitHub Desktop.
Save eyalcohen4/488cc46b5a63751ac1fe668d00d22c2b to your computer and use it in GitHub Desktop.
ndjson-with-axios.ts
import axios from 'axios';
import { parse } from 'ndjson';
async function fetchNDJSON(url: string): Promise<void> {
try {
const response = await axios({
method: 'get',
url: url,
responseType: 'stream'
});
const stream = response.data;
stream.pipe(parse())
.on('data', (obj: any) => {
console.log('Parsed JSON object:', obj);
})
.on('error', (error: any) => {
console.error('Error parsing NDJSON:', error);
});
} catch (error) {
console.error('Error fetching NDJSON:', error);
}
}
fetchNDJSON('https://example.com/ndjson-endpoint');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment