Security News
Cloudflare Adds Security.txt Setup Wizard
Cloudflare has launched a setup wizard allowing users to easily create and manage a security.txt file for vulnerability disclosure on their websites.
The 'wreck' npm package is a powerful HTTP client library for Node.js, designed to make HTTP requests and handle responses with ease. It is part of the hapi ecosystem and provides a simple API for making HTTP requests, handling redirects, and managing cookies.
Making HTTP GET Requests
This feature allows you to make HTTP GET requests to a specified URL. The code sample demonstrates how to use Wreck to fetch data from a URL and log the response status code and payload.
const Wreck = require('wreck');
async function fetchData(url) {
try {
const { res, payload } = await Wreck.get(url);
console.log('Response:', res.statusCode);
console.log('Payload:', payload.toString());
} catch (err) {
console.error('Error:', err);
}
}
fetchData('https://jsonplaceholder.typicode.com/posts/1');
Making HTTP POST Requests
This feature allows you to make HTTP POST requests with a payload. The code sample demonstrates how to use Wreck to send data to a URL and log the response status code and payload.
const Wreck = require('wreck');
async function postData(url, data) {
try {
const { res, payload } = await Wreck.post(url, { payload: data });
console.log('Response:', res.statusCode);
console.log('Payload:', payload.toString());
} catch (err) {
console.error('Error:', err);
}
}
postData('https://jsonplaceholder.typicode.com/posts', { title: 'foo', body: 'bar', userId: 1 });
Handling Redirects
This feature allows you to handle HTTP redirects automatically. The code sample demonstrates how to use Wreck to follow up to 3 redirects when making a GET request.
const Wreck = require('wreck');
async function fetchWithRedirect(url) {
try {
const { res, payload } = await Wreck.get(url, { redirects: 3 });
console.log('Response:', res.statusCode);
console.log('Payload:', payload.toString());
} catch (err) {
console.error('Error:', err);
}
}
fetchWithRedirect('http://example.com');
Managing Cookies
This feature allows you to manage cookies for your HTTP requests. The code sample demonstrates how to use Wreck to set a cookie and include it in a GET request.
const Wreck = require('wreck');
async function fetchWithCookies(url) {
const jar = Wreck.jar();
jar.setCookie('session=abc123', url);
try {
const { res, payload } = await Wreck.get(url, { cookies: jar });
console.log('Response:', res.statusCode);
console.log('Payload:', payload.toString());
} catch (err) {
console.error('Error:', err);
}
}
fetchWithCookies('http://example.com');
Axios is a popular promise-based HTTP client for the browser and Node.js. It provides a simple and intuitive API for making HTTP requests and handling responses. Compared to Wreck, Axios has a larger community and more extensive documentation, making it a go-to choice for many developers.
Node-fetch is a lightweight module that brings the Fetch API to Node.js. It is designed to be a minimalistic and straightforward HTTP client. Compared to Wreck, node-fetch is more focused on providing a fetch-like experience in Node.js, making it a good choice for developers familiar with the Fetch API.
Request is a simplified HTTP client for Node.js, designed to be easy to use and flexible. It has been deprecated, but it remains widely used in many legacy projects. Compared to Wreck, Request offers a more extensive set of features and options, but its deprecation means it is no longer actively maintained.
HTTP Client Utilities
Lead Maintainer: Wyatt Preul
var Wreck = require('wreck');
Wreck.get('https://google.com/', function (err, res, payload) {
/* do stuff */
});
var Wreck = require('wreck');
var method = 'GET'; // GET, POST, PUT, DELETE
var uri = 'https://google.com/';
var readableStream = Wreck.toReadableStream('foo=bar');
// all attributes are optional
var options = {
payload: readableStream || 'foo=bar' || new Buffer('foo=bar'),
headers: { /* http headers */ },
redirects: 3,
timeout: 1000, // 1 second, default: unlimited
maxBytes: 1048576, // 1 MB, default: unlimited
rejectUnauthorized: true || false,
downstreamRes: null,
agent: null // Node Core http.Agent
};
var optionalCallback = function (err, res) {
// buffer the response stream
Wreck.read(res, null, function (err, body) {
/* do stuff */
});
};
Wreck.request(method, uri, options, optionalCallback);
request(method, uri, [options, [callback]])
Initiate an HTTP request.
method
- A string specifying the HTTP request method, defaulting to 'GET'.uri
- The URI of the requested resource.options
- An optional configuration object. To omit this argument but still
use a callback, pass null
in this position. The options object supports the
following optional keys:
payload
- The request body as string, Buffer, or Readable Stream.headers
- An object containing request headers.rejectUnauthorized
- TLS flag indicating
whether the client should reject a response from a server with invalid certificates.redirects
- The maximum number of redirects to follow.agent
- Node Core http.Agent.timeout
- The number of milliseconds to wait without receiving a response
before aborting the request. Defaults to unlimited.callback
- The optional callback function using the signature function (err, response)
where:
err
- Any error that may have occurred during the handling of the request.response
- The HTTP Incoming Message
object, which is also a readable stream.read(response, options, callback)
response
- An HTTP Incoming Message object.options
- null
or a configuration object with the following optional keys:
timeout
- The number of milliseconds to wait while reading data before
aborting handling of the response. Defaults to unlimited.json
- A flag indicating whether the payload should be parsed as JSON
if the response indicates a JSON content-type.maxBytes
- The maximum allowed response payload size. Defaults to unlimited.callback
- The callback function using the signature function (err, payload)
where:
err
- Any error that may have occurred while reading the response.payload
- The payload in the form of a Buffer or (optionally) parsed JavaScript object (JSON).get(uri, [options], callback)
Convenience method for GET operations.
uri
- The URI of the requested resource.options
- Optional config object containing settings for both request
and
read
operations.callback
- The callback function using the signature function (err, response, payload)
where:
err
- Any error that may have occurred during handling of the request.response
- The HTTP Incoming Message
object, which is also a readable stream.payload
- The payload in the form of a Buffer or (optionally) parsed JavaScript object (JSON).post(uri, [options], callback)
Convenience method for POST operations.
uri
- The URI of the requested resource.options
- Optional config object containing settings for both request
and
read
operations.callback
- The callback function using the signature function (err, response, payload)
where:
err
- Any error that may have occurred during handling of the request.response
- The HTTP Incoming Message
object, which is also a readable stream.payload
- The payload in the form of a Buffer or (optionally) parsed JavaScript object (JSON).put(uri, [options], callback)
Convenience method for PUT operations.
uri
- The URI of the requested resource.options
- Optional config object containing settings for both request
and
read
operations.callback
- The callback function using the signature function (err, response, payload)
where:
err
- Any error that may have occurred during handling of the request.response
- The HTTP Incoming Message
object, which is also a readable stream.payload
- The payload in the form of a Buffer or (optionally) parsed JavaScript object (JSON).delete(uri, [options], callback)
Convenience method for DELETE operations.
uri
- The URI of the requested resource.options
- Optional config object containing settings for both request
and
read
operations.callback
- The callback function using the signature function (err, response, payload)
where:
err
- Any error that may have occurred during handling of the request.response
- The HTTP Incoming Message
object, which is also a readable stream.payload
- The payload in the form of a Buffer or (optionally) parsed JavaScript object (JSON).toReadableStream(payload, [encoding])
Creates a readable stream for the provided payload and encoding.
payload
- The Buffer or string to be wrapped in a readable stream.encoding
- The encoding to use. Must be a valid Buffer encoding, such as 'utf8' or 'ascii'.var stream = Wreck.toReadableStream(new Buffer('Hello', 'ascii'), 'ascii');
var read = stream.read();
// read -> 'Hello'
parseCacheControl(field)
Parses the provided cache-control request header value into an object containing
a property for each directive and it's value. Boolean directives, such as "private"
or "no-cache" will be set to the boolean true
.
field
- The header cache control value to be parsed.var result = Wreck.parseCacheControl('private, max-age=0, no-cache');
// result.private -> true
// result['max-age'] -> 0
// result['no-cache'] -> true
FAQs
HTTP Client Utilities
The npm package wreck receives a total of 171,816 weekly downloads. As such, wreck popularity was classified as popular.
We found that wreck demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 4 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.
Security News
Cloudflare has launched a setup wizard allowing users to easily create and manage a security.txt file for vulnerability disclosure on their websites.
Security News
The Socket Research team breaks down a malicious npm package targeting the legitimate DOMPurify library. It uses obfuscated code to hide that it is exfiltrating browser and crypto wallet data.
Security News
ENISA’s 2024 report highlights the EU’s top cybersecurity threats, including rising DDoS attacks, ransomware, supply chain vulnerabilities, and weaponized AI.