Research
Security News
Threat Actor Exposes Playbook for Exploiting npm to Build Blockchain-Powered Botnets
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.
The gaxios npm package is a lightweight HTTP client based on Axios but with a smaller footprint. It is designed to work in both browser and node environments, providing a simple way to make HTTP requests. It supports all HTTP request methods, automatic JSON data transformation, and custom configuration for requests.
GET Request
This feature allows you to make GET requests to retrieve data from a specified resource.
const { request } = require('gaxios');
async function getUser() {
try {
const response = await request({ url: 'https://api.example.com/user', method: 'GET' });
console.log(response.data);
} catch (error) {
console.error(error);
}
}
getUser();
POST Request
This feature allows you to make POST requests to send data to a server to create/update a resource.
const { request } = require('gaxios');
async function createUser(userData) {
try {
const response = await request({
url: 'https://api.example.com/user',
method: 'POST',
data: userData
});
console.log(response.data);
} catch (error) {
console.error(error);
}
}
createUser({ name: 'New User', email: 'newuser@example.com' });
Interceptors
Interceptors allow you to run your code or modify the request and/or response before the request is sent or after the response is received.
const { Gaxios } = require('gaxios');
const instance = new Gaxios({
baseURL: 'https://api.example.com'
});
// Add a request interceptor
instance.interceptors.request.use(config => {
// Do something before request is sent
config.headers['Authorization'] = 'Bearer token';
return config;
});
// Add a response interceptor
instance.interceptors.response.use(response => {
// Do something with response data
return response;
}, error => {
// Handle error
return Promise.reject(error);
});
Custom Configuration
Custom configuration allows you to specify various options for the HTTP request, such as headers, query parameters, timeout, and more.
const { request } = require('gaxios');
async function getCustomData() {
try {
const response = await request({
url: 'https://api.example.com/data',
method: 'GET',
timeout: 5000,
headers: { 'X-Custom-Header': 'foobar' }
});
console.log(response.data);
} catch (error) {
console.error(error);
}
}
getCustomData();
Axios is a popular HTTP client for the browser and node.js. It is similar to gaxios but has a larger footprint and more features, such as interceptors, automatic transforms for JSON data, and client-side support for protecting against XSRF.
node-fetch is a light-weight module that brings the Fetch API to Node.js. It is similar to gaxios in terms of being promise-based and lightweight but does not have built-in support for interceptors or automatic JSON data transformation.
Got is a human-friendly and powerful HTTP request library for Node.js. It offers stream support, promises, and advanced features like retries and timeouts. It is more feature-rich compared to gaxios but also has a larger footprint.
Superagent is a small progressive client-side HTTP request library, and Node.js module with the same API, sporting many high-level HTTP client features. It is similar to gaxios but includes more built-in features like form data handling and file uploads.
An HTTP request client that provides an
axios
like interfance over top ofnode-fetch
. Only really useful if you're trying to migrate from axios to the fetch.
$ npm install gaxios
const {request} = require('gaxios');
const res = await request({
url: 'https://www.googleapis.com/discovery/v1/apis/'
});
Gaxios supports setting default properties both on the default instance, and on additional instances. This is often useful when making many requests to the same domain with the same base settings. For example:
const gaxios = require('gaxios');
gaxios.instance.defaults = {
baseURL: 'https://example.com'
headers: {
Authorization: 'SOME_TOKEN'
}
}
gaxios.request({url: '/data'}).then(...);
{
// The url to which the request should be sent. Required.
url: string,
// The HTTP method to use for the request. Defaults to `GET`.
method: 'GET',
// The base Url to use for the request. Prepended to the `url` property above.
baseURL: 'https://example.com';
// The HTTP methods to be sent with the request.
headers: { 'some': 'header' },
// The data to base64 encode and send in the body of the request.
data: {
some: 'data'
},
// The max size of the http response content in bytes allowed.
// Defaults to `0`, which is the same as unset.
maxContentLength: 2000,
// The max number of HTTP redirects to follow.
// Defaults to 100.
maxRedirects: 100,
// The querystring parameters that will be encoded using `qs` and
// appended to the url
params: {
querystring: 'parameters'
},
// By default, we use the `querystring` package in node core to serialize
// querystring parameters. You can override that and provide your
// own implementation.
paramsSerializer: (params) => {
return qs.stringify(params);
},
// The timeout for the HTTP request. Defaults to 0.
timeout: 1000,
// Optional method to override making the actual HTTP request. Useful
// for writing tests.
adapter?: (options) => {
return {
data: 'your data'
}
};
// The expected return type of the request. Options are:
// json | stream | blob | arraybuffer | text
// Defaults to `json`.
responseType: 'json',
// The node.js http agent to use for the request.
agent: someHttpsAgent,
// Custom function to determine if the response is valid based on the
// status code. Defaults to (>= 200 && < 300)
validateStatus: (status: number) => true,
// Configuration for retrying of requests.
retryConfig: {
// The number of times to retry the request. Defaults to 3.
retry?: number;
// The number of retries already attempted.
currentRetryAttempt?: number;
// The amount of time to initially delay the retry. Defaults to 100.
retryDelay?: number;
// The HTTP Methods that will be automatically retried.
// Defaults to ['GET','PUT','HEAD','OPTIONS','DELETE']
httpMethodsToRetry?: string[];
// The HTTP response status codes that will automatically be retried.
// Defaults to: [[100, 199], [429, 429], [500, 599]]
statusCodesToRetry?: number[][];
// Function to invoke when a retry attempt is made.
onRetryAttempt?: (err: GaxiosError) => void;
// Function to invoke which determines if you should retry
shouldRetry?: (err: GaxiosError) => boolean;
// When there is no response, the number of retries to attempt. Defaults to 2.
noResponseRetries?: number;
},
// Enables default configuration for retries.
retry: boolean,
// Cancelling a request requires the `abort-controller` library.
// See https://github.com/bitinn/node-fetch#request-cancellation-with-abortsignal
signal?: AbortSignal
}
FAQs
A simple common HTTP client specifically for Google APIs and services.
The npm package gaxios receives a total of 12,586,707 weekly downloads. As such, gaxios popularity was classified as popular.
We found that gaxios demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer 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
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.
Security News
NVD’s backlog surpasses 20,000 CVEs as analysis slows and NIST announces new system updates to address ongoing delays.
Security News
Research
A malicious npm package disguised as a WhatsApp client is exploiting authentication flows with a remote kill switch to exfiltrate data and destroy files.