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

XMLHttpRequest for Node


Version published
Weekly downloads
1.3M
increased by5.02%
Maintainers
1
Install size
22.0 kB
Created
Weekly downloads
 

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.

Note: This library currently conforms to XMLHttpRequest 1. Version 2.0 will target XMLHttpRequest Level 2.

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();

Note: use the lowercase string "xmlhttprequest" in your require(). On case-sensitive systems (eg Linux) using uppercase letters won't work.

Versions

Prior to 1.4.0 version numbers were arbitrary. From 1.4.0 on they conform to the standard major.minor.bugfix. 1.x shouldn't necessarily be considered stable just because it's above 0.x.

Since the XMLHttpRequest API is stable this library's API is stable as well. Major version numbers indicate significant core code changes. Minor versions indicate minor core code changes or better conformity to the W3C spec.

License

MIT license. See LICENSE for full details.

Supports

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

Known Issues / Missing Features

For a list of open issues or to report your own visit the github issues page.

  • Local file access may have unexpected results for non-UTF8 files
  • Synchronous requests don't set headers properly
  • Synchronous requests freeze node while waiting for response (But that's what you want, right? Stick with async!).
  • Some events are missing, such as abort
  • Cookies aren't persisted between requests
  • Missing XML support

Keywords

FAQs

Last updated on 11 Oct 2015

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc