Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
@whatwg-node/fetch
Advanced tools
@whatwg-node/fetch is an npm package that provides a WHATWG Fetch API implementation for Node.js. It allows developers to use the familiar Fetch API, which is standard in web browsers, in a Node.js environment. This package is useful for making HTTP requests, handling responses, and working with various types of data formats.
Basic Fetch Request
This feature allows you to make a basic HTTP GET request to a specified URL and handle the response. The example fetches a post from a placeholder API and logs the JSON response.
const fetch = require('@whatwg-node/fetch');
fetch('https://jsonplaceholder.typicode.com/posts/1')
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error('Error:', error));
POST Request with JSON Body
This feature allows you to make an HTTP POST request with a JSON body. The example sends a new post to the placeholder API and logs the JSON response.
const fetch = require('@whatwg-node/fetch');
const data = { title: 'foo', body: 'bar', userId: 1 };
fetch('https://jsonplaceholder.typicode.com/posts', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(data)
})
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error('Error:', error));
Handling Different Response Types
This feature demonstrates how to handle different response types and errors. The example checks if the response is OK before parsing it as JSON, and throws an error if the response is not OK.
const fetch = require('@whatwg-node/fetch');
fetch('https://jsonplaceholder.typicode.com/posts/1')
.then(response => {
if (response.ok) {
return response.json();
} else {
throw new Error('Network response was not ok');
}
})
.then(data => console.log(data))
.catch(error => console.error('Error:', error));
node-fetch is a lightweight module that brings `window.fetch` to Node.js. It is widely used and has a similar API to the Fetch API in browsers. Compared to @whatwg-node/fetch, node-fetch is more mature and has a larger user base.
axios is a promise-based HTTP client for Node.js and the browser. It provides a more feature-rich API compared to the Fetch API, including request and response interceptors, automatic JSON transformation, and more. It is more versatile but also more complex than @whatwg-node/fetch.
got is a human-friendly and powerful HTTP request library for Node.js. It supports many advanced features like retries, streams, and hooks. Compared to @whatwg-node/fetch, got offers more advanced features and better error handling.
FAQs
Cross Platform Smart Fetch Ponyfill
We found that @whatwg-node/fetch demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.