Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

then-request

Package Overview
Dependencies
Maintainers
1
Versions
27
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

then-request

A request library that returns promises, inspired by request

  • 2.1.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
892K
decreased by-0.25%
Maintainers
1
Weekly downloads
 
Created

What is then-request?

The then-request npm package is a simplified HTTP client for Node.js that uses promises. It allows you to make HTTP requests and handle responses in a straightforward manner using promises, making it easier to work with asynchronous code.

What are then-request's main functionalities?

Basic GET Request

This feature allows you to make a basic GET request to a specified URL. The response is handled using a promise, and the response body is logged to the console.

const request = require('then-request');

request('GET', 'https://jsonplaceholder.typicode.com/posts/1')
  .done(function (res) {
    console.log(res.getBody());
  });

POST Request with JSON Body

This feature allows you to make a POST request with a JSON body. The request sends data to the specified URL, and the response is handled using a promise.

const request = require('then-request');

request('POST', 'https://jsonplaceholder.typicode.com/posts', {
  json: {
    title: 'foo',
    body: 'bar',
    userId: 1
  }
}).done(function (res) {
  console.log(res.getBody());
});

Handling Response Headers

This feature allows you to access and log the response headers from an HTTP request. The headers are available in the response object.

const request = require('then-request');

request('GET', 'https://jsonplaceholder.typicode.com/posts/1')
  .done(function (res) {
    console.log(res.headers);
  });

Handling Errors

This feature demonstrates how to handle errors in HTTP requests. If the request fails, the error is caught and logged to the console.

const request = require('then-request');

request('GET', 'https://jsonplaceholder.typicode.com/invalid-url')
  .done(function (res) {
    console.log(res.getBody());
  }, function (err) {
    console.error('Request failed:', err);
  });

Other packages similar to then-request

FAQs

Package last updated on 26 Jan 2015

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