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

httpie

Package Overview
Dependencies
Maintainers
1
Versions
26
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

httpie

A lightweight, Promise-based wrapper for Node.js HTTP requests~!

  • 1.0.2
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
12K
decreased by-17.5%
Maintainers
1
Weekly downloads
 
Created
Source
httpie
version travis codecov downloads
A Node.js HTTP client as easy as pie!

Features

  • Promise- based HTTP requestor
  • Works with HTTP and HTTPS protocols
  • Automatically handles JSON requests and responses
  • Extremely lightweight with no dependencies – 634 bytes!
  • Includes aliases for common HTTP verbs: get, post, put, patch, and del

Additionally, this module is delivered as:

Install

$ npm install --save httpie

Usage

Note: The async syntax is for demo purposes – you may use Promises in a Node 6.x environment too!

import { get, post } from 'httpie';

try {
  const { data } = await get('https://pokeapi.co/api/v2/pokemon/1');

  // Demo: Endpoint will echo what we've sent
  const res = await post('https://jsonplaceholder.typicode.com/posts', {
    body: {
      id: data.id,
      name: data.name,
      number: data.order,
      moves: data.moves.slice(0, 6)
    }
  });

  console.log(res.statusCode); //=> 201
  console.log(res.data); //=> { id: 1, name: 'bulbasaur', number: 1, moves: [{...}, {...}] }
} catch (err) {
  console.error('Error!', err.statusCode, err.message);
  console.error('~> headers:', err.headers);
  console.error('~> data:', err.data);
}

API

send(method, url, opts={})

Returns: Promise

Any httpie.send request (and its aliases) will always return a Promise.

If the response's statusCode is 400 or above, this Promise will reject with a formatted error – see Error Handling. Otherwise, the Promise will resolve with the full ClientRequest stream.

The resolved response will receive a new data key, which will contain the response's full payload. Should the response return JSON content, then httpie will parse it and the res.data value will be the resulting JSON object!

method

Type: String

The HTTP method name – it must be uppercase!

url

Type: String or URL

If url is a string, it is automatically parsed with url.parse() into an object.

opts.body

Type: Mixed
Default: undefined

The request's body, can be of any type!

Any non-Buffer objects will be converted into a JSON string and the appropriate Content-Type header will be attached.

Additionally, httpie will always set a value for the Content-Length header!

opts.headers

Type: Object
Default: {}

The custom headers to send with your request.

opts.redirect

Type: Boolean
Default: true

Whether or not redirect responses should be followed automatically.

Note: This may only happen with a 3xx status and if the response had a Location header.

get(url, opts={})

Alias for send('GET', url, opts).

post(url, opts={})

Alias for send('POST', url, opts).

put(url, opts={})

Alias for send('PUT', url, opts).

patch(url, opts={})

Alias for send('PATCH', url, opts).

del(url, opts={})

Alias for send('DELETE', url, opts).

Error Handling

All responses with statusCode >= 400 will result in a rejected httpie request. When this occurs, an Error instance is formatted with complete information:

  • err.messageString – Identical to err.statusMessage;
  • err.statusMessageString – The response's statusMessage value;
  • err.statusCodeNumber – The response's statusCode value;
  • err.headersObject – The response's headers object;
  • err.dataMixed – The response's payload;

Important: The error's data property may also be parsed to a JSON object, according to the response's headers.

import { get } from 'httpie';

get('https://example.com/404').catch(err => {
  console.error(`(${err.statusCode}) ${err.message}`)
  console.error(err.headers['content-type']);
  console.error(`~> ${err.data}`);
});
//=> "(404) Not Found"
//=> "text/html; charset=UTF-8"
//=> ~> <?xml version="1.0" encoding="iso-8859-1"?>\n<!DOCTYPE html ...</body>\n</html>

License

MIT © Luke Edwards

Keywords

FAQs

Package last updated on 02 Feb 2019

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