Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

http-promises

philcockfield38MIT1.2.2

Consistent HTTP request API on both server and browser using promises.

http, xhr, promise, rest

readme

http-promises

Build Status

Consistent HTTP request API on both server and browser using promises.

API

  • http.get(url)
  • http.post(url, { payload })
  • http.put(url, { payload })
  • http.delete(url)
  • http.header(key, value)

Usage

Server

import http from "http-promises/server";

let URL = "http://domain.com/foo";
http.get(URL)
  .then(result => { ... })
  .catch(err => { throw err; });

http.put(URL, { foo: 123 })
  .then(result => { ... })
  .catch(err => { throw err; });

Headers

Add headers by calling the chainable header(key, value) method:


http
  .header("Context-Type", "application/json")
  .header("My-Header", 1234)
  .get("/endpoint")
  .then( ... )

Adding headers is immutable. Each call to header returns a fresh API. The root http API is not effected.

Browser (Client)

Using it within the browser is exactly the same as the server, just require "http-promises/browser"

import http from "http-promises/browser";

http.get("/foo")
  .then(result => { ... })
  .catch(err => { throw err; });

Test

npm test

License: MIT

changelog

Change Log

All notable changes to this project will be documented in this file. This project adheres to Semantic Versioning.

[Unreleased] - YYYY-MM-DD

Added

Changed

Deprecated

Removed

Fixed

Security

[1.2.0] - 2016-01-24

Changed

[1.1.0] - 2015-11-4

Changed

The result from the promise returned from HTTP methods (get/push/post/delete) is no longer the direct data result, but rather an object, allowing headers to be returned:

{
  data: ... // The response data (formerly this was the 'result' value),
  headers: ... // The response header data.
}