{"library":"nodemailer-fetch","title":"Nodemailer HTTP Fetcher","description":"nodemailer-fetch is a focused utility library designed to efficiently fetch HTTP URL contents. While primarily created to complement the Nodemailer ecosystem by providing a mechanism to retrieve remote content for email templates or attachments, it serves as a standalone HTTP client for general-purpose requests. The current stable version is 2.1.0. It generally follows a stable, on-demand release cadence typical for a mature utility module. Its key differentiators include robust handling of redirects (up to 5 by default), automatic gzip decompression, comprehensive cookie management with domain specificity, and built-in support for basic authentication, though with a notable caveat regarding redirects. It offers configurable TLS options and allows for custom HTTP methods (GET, POST), headers, and request bodies, making it flexible for various data fetching scenarios.","language":"javascript","status":"active","last_verified":"Tue Apr 21","install":{"commands":["npm install nodemailer-fetch"],"cli":null},"imports":["const fetch = require('nodemailer-fetch');"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"const fetch = require('nodemailer-fetch');\n\n// Example 1: Basic GET request and pipe response to stdout\nconsole.log('Fetching a public URL and piping response to stdout...');\nfetch('https://jsonplaceholder.typicode.com/posts/1')\n    .then(response => {\n        if (!response || response.statusCode >= 400) {\n            throw new Error(`HTTP Error: ${response ? response.statusCode : 'No response'}`);\n        }\n        console.log(`\nStatus Code: ${response.statusCode}`);\n        console.log('Headers:', response.headers);\n        console.log('Body:');\n        return response;\n    })\n    .then(response => {\n        response.pipe(process.stdout);\n        response.on('end', () => console.log('\\n--- Basic GET fetch complete ---'));\n    })\n    .catch(error => console.error('Error during basic GET fetch:', error.message));\n\n// Example 2: POST request with custom body, headers, and timeout\nconsole.log('\\nSending a POST request with custom options...');\nfetch('https://jsonplaceholder.typicode.com/posts', {\n    method: 'POST',\n    body: {\n        title: 'foo',\n        body: 'bar',\n        userId: 1\n    },\n    contentType: 'application/json', // Override default x-www-form-urlencoded\n    headers: {\n        'Accept': 'application/json',\n        'User-Agent': 'NodemailerFetchDemo/1.0'\n    },\n    timeout: 5000, // 5 seconds timeout for the connection\n    allowErrorResponse: true // Process response even if status code is non-2xx\n})\n.then(response => {\n    if (!response) {\n        throw new Error('No response received for POST request');\n    }\n    console.log(`\nPOST Status Code: ${response.statusCode}`);\n    return new Promise((resolve, reject) => {\n        let data = '';\n        response.on('data', chunk => data += chunk);\n        response.on('end', () => {\n            try {\n                resolve(JSON.parse(data));\n            } catch (e) {\n                reject(new Error(`Failed to parse JSON response: ${data.substring(0, 100)}...`));\n            }\n        });\n        response.on('error', reject);\n    });\n})\n.then(json => {\n    console.log('POST Response Body:', json);\n    console.log('--- Custom POST fetch complete ---');\n})\n.catch(error => console.error('Error during custom POST fetch:', error.message));","lang":"javascript","description":"Demonstrates basic HTTP GET and more complex POST requests, including piping the response, handling custom headers, body serialization, and setting timeouts.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}