Socket
Socket
Sign inDemoInstall

xhr2

Package Overview
Dependencies
Maintainers
1
Versions
14
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

xhr2

XMLHttpRequest emulation for node.js


Version published
Weekly downloads
782K
decreased by-18.73%
Maintainers
1
Weekly downloads
 
Created

What is xhr2?

The xhr2 npm package is a server-side implementation of the XMLHttpRequest API, which is commonly used in web browsers for making HTTP requests. This package allows Node.js applications to make HTTP requests using the familiar XMLHttpRequest interface.

What are xhr2's main functionalities?

Basic GET Request

This feature allows you to make a basic GET request to a specified URL. The code sample demonstrates how to open a connection, send the request, and handle the response.

const XMLHttpRequest = require('xhr2');
const xhr = new XMLHttpRequest();
xhr.open('GET', 'https://jsonplaceholder.typicode.com/posts/1', true);
xhr.onload = function () {
  if (xhr.status >= 200 && xhr.status < 300) {
    console.log(xhr.responseText);
  } else {
    console.error('Request failed with status:', xhr.status);
  }
};
xhr.send();

POST Request with Data

This feature allows you to make a POST request with data. The code sample demonstrates how to set the request method to POST, set the appropriate headers, and send JSON data in the request body.

const XMLHttpRequest = require('xhr2');
const xhr = new XMLHttpRequest();
xhr.open('POST', 'https://jsonplaceholder.typicode.com/posts', true);
xhr.setRequestHeader('Content-Type', 'application/json;charset=UTF-8');
xhr.onload = function () {
  if (xhr.status >= 200 && xhr.status < 300) {
    console.log(xhr.responseText);
  } else {
    console.error('Request failed with status:', xhr.status);
  }
};
xhr.send(JSON.stringify({ title: 'foo', body: 'bar', userId: 1 }));

Handling Errors

This feature allows you to handle errors that may occur during the request. The code sample demonstrates how to handle both HTTP status errors and network errors.

const XMLHttpRequest = require('xhr2');
const xhr = new XMLHttpRequest();
xhr.open('GET', 'https://jsonplaceholder.typicode.com/invalid-url', true);
xhr.onload = function () {
  if (xhr.status >= 200 && xhr.status < 300) {
    console.log(xhr.responseText);
  } else {
    console.error('Request failed with status:', xhr.status);
  }
};
xhr.onerror = function () {
  console.error('Network error occurred');
};
xhr.send();

Other packages similar to xhr2

Keywords

FAQs

Package last updated on 06 Jan 2017

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc