Socket
Socket
Sign inDemoInstall

xmlhttprequest

Package Overview
Dependencies
0
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

xmlhttprequest


Version published
Maintainers
1
Install size
15.6 kB
Created

Package description

What is xmlhttprequest?

The xmlhttprequest npm package is a JavaScript library that allows you to perform HTTP client functionality, such as making GET and POST requests to servers. It is designed to mimic the behavior of the native XMLHttpRequest object provided by web browsers, making it useful for server-side applications or testing where the native object is not available.

What are xmlhttprequest's main functionalities?

Performing a GET request

This code sample demonstrates how to perform a simple GET request to retrieve data from a specified URL.

var XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest;
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
  if (xhr.readyState == 4 && xhr.status == 200) {
    console.log(xhr.responseText);
  }
};
xhr.open('GET', 'http://example.com', true);
xhr.send();

Performing a POST request

This code sample shows how to perform a POST request to send JSON data to a server.

var XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest;
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
  if (xhr.readyState == 4 && xhr.status == 200) {
    console.log(xhr.responseText);
  }
};
xhr.open('POST', 'http://example.com', true);
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.send(JSON.stringify({ key: 'value' }));

Setting request headers

This code sample illustrates how to set custom HTTP headers for a request.

var XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest;
var xhr = new XMLHttpRequest();
xhr.open('GET', 'http://example.com', true);
xhr.setRequestHeader('X-Custom-Header', 'value');
xhr.send();

Handling errors

This code sample demonstrates how to handle network errors that may occur during the request.

var XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest;
var xhr = new XMLHttpRequest();
xhr.onerror = function() {
  console.error('Request failed');
};
xhr.open('GET', 'http://example.com', true);
xhr.send();

Other packages similar to xmlhttprequest

Readme

Source

node-XMLHttpRequest

node-XMLHttpRequest is a wrapper for the built-in http client to emulate the browser XMLHttpRequest object.

This can be used with JS designed for browsers to improve reuse of code and allow the use of existing libraries.

Usage

Here's how to include the module in your project and use as the browser-based XHR object.

var XMLHttpRequest = require("XMLHttpRequest").XMLHttpRequest;
var xhr = new XMLHttpRequest();

Refer to W3C specs for XHR methods.

Supports

  • Async and synchronous requests
  • GET, POST, and PUT requests
  • All native methods (open, send, abort, getRequestHeader, getAllRequestHeaders)
  • Requests to all domains

TODO

  • Add basic authentication
  • Additional unit tests
  • Possibly move from http to tcp for more flexibility
  • DELETE requests aren't working
  • XML parsing

FAQs

Last updated on 01 Nov 2011

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

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc