npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@trex-arms/httpie

v2.0.0-next.16

Published

A fork of lukeed/httpie

Downloads

5

Readme

Differences from httpie

  • Added err.request_uri and err.request_body
  • Handles leading byte order marks in JSON responses

Features

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

Additionally, this module is delivered as:

Install

$ npm install --save @trex-arms/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.

opts.reviver

Type: Function Default: undefined

An optional function that's passed directly to JSON.parse, allowing you transform aspects of the response data before the httpie request resolves.

Note: This will only run if httpie detects that JSON is contained in the response!

opts.timeout

Type: Integer Default: undefined

The time, in milliseconds, before automatically terminating the request.

When the request exceeds this limit, httpie rejects with an err<Error>, adding a truthy err.timeout value.

Important: There is a slight behavioral difference between the Node & browser versions! In the server, the timeout value does not propagate to any redirects. In the browser, the timeout value will not reset during redirects.

opts.withCredentials

Type: Boolean Default: false

Whether or not cross-site requests should include credentials (such as cookies). This value is passed directly to XMLHttpRequest.withCredentials.

Important: This is for the browser version only!

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;
  • err.request_uriString or URL – The request's URI;
  • err.request_bodyString – The request's payload;

Additionally, errors that are a result of a timeout expiration will have a truthy err.timeout value.

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.uri);
  console.error(`~> ${err.data}`);
});
//=> "(404) Not Found"
//=> "text/html; charset=UTF-8"
//=> "https://example.com/404"
//=> ~> <?xml version="1.0" encoding="iso-8859-1"?>\n<!DOCTYPE html ...</body>\n</html>

License

MIT © Luke Edwards