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

okhttp

v1.1.0

Published

http client for node, inspired by Square's Android OkHttp

Downloads

75

Readme

node-OkHttP

a lightweight HTTP library for Node.js inspired by Square's Android OkHttp Builder patterns and flavours for constructing a legit HTTP requests of all types. Written entirely in EcmaScript 6 (Harmony) and transpiled with Babel.

NPM

How to use

  • npm install okhttp
  • npm install --production okhttp (if you have the following modules installed globally babel-cli, babel-plugin-transform-object-assign,babel-preset-es2015, then use)

Features

  • supports all HTTP methods.
  • supports MultiPart requests.
  • includes Builders for:
    • Request builder.
    • Form Encoding body builder.
    • Multipart body builder.
    • Mime builder.
    • Request Body composer.
  • use Promise api for intuitive async.
  • most important: quick and easy to use.

Guide

const okhttp                = require('okhttp');

var MimeBuilder             = okhttp.MimeBuilder;
var Request                 = okhttp.Request;
var RequestBody             = okhttp.RequestBody;
var RequestBuilder          = okhttp.RequestBuilder;
var FormEncodingBuilder     = okhttp.FormEncodingBuilder;
var MultiPartBuilder        = okhttp.MultiPartBuilder;

/**
 * @param msg a {data, response, request} Object
 */
private function onComplete(msg):void {
    console.log('data ' + msg.data + ', response ' + msg.response.statusCode + ', request ' + msg.request.method);
}

private function onError(err):void {
    console.error(err);
}

1) Simple textual GET request

new RequestBuilder().GET('http://google.com').buildAndExecute().then(onComplete).catch(onError);

2) Simple binary GET request

  • data returns as a buffer
new RequestBuilder().GET('http://maps.google.com/mapfiles/kml/pushpin/blue-pushpin.png').bufferResponse().buildAndExecute().then(onComplete).catch(onError);

2) Simple JSON POST request

new RequestBuilder().url('http://httpbin.org/post')
                    .POST(RequestBody.create({a:'a1', b:'b1'}, new MimeBuilder().contentType('application/json', 'charset', 'utf8').build()))
                    .buildAndExecute().then(onComplete).catch(onError);

3) Form Encoding POST (www-form-urlencoded) request

let fe_body = new FormEncodingBuilder().add('key1', 'value1').add('key2', 'value2').build();

new RequestBuilder().url('http://httpbin.org/post').POST(fe_body).buildAndExecute()
                    .then(onComplete).catch(onError);

4) MultiPart request:

let json    = JSON.stringify({title:'test'});
var image   = fs.readFileSync(path.resolve(__dirname, 'test.jpg'));

let mp_body = new MultiPartBuilder().addPart(RequestBody.create(json, 'Content-Type: application/json; charset=UTF-8'))
                                    .addPart(RequestBody.create(image, new MimeBuilder().contentType('image/jpeg').contentTransferEncoding('binary').build()))
                                    .type(MultiPartBuilder.FORMDATA).build();

new RequestBuilder().url('https://www.googleapis.com/upload/drive/v2/files?uploadType=multipart')
                    .header('Authorization', 'Bearer OAUTH2_TOKEN_HERE')
                    .POST(mp_body).buildAndExecute().then(onComplete).catch(onError);

5) POST image request

  • send an image into Google Glass Mirror API
public function uploadImage(image, itemId, oauthToken):void {

      var body = RequestBody.create(image, 'Content-Type: image/png');
      
      new RequestBuilder().url("https://www.googleapis.com/upload/mirror/v1/timeline" + "/" + itemId + "/attachments")
                          .query("access_token", oauthToken)
                          .POST(body).build().execute()
                          .then(onComplete).catch(onError);
}

Dev Dependencies

Terms

Contact Author