• Stars
    star
    358
  • Rank 118,855 (Top 3 %)
  • Language
    JavaScript
  • License
    Apache License 2.0
  • Created almost 13 years ago
  • Updated over 7 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Browser library compatible with Node.js request package

Browser Request: The easiest HTTP library you'll ever see

Browser Request is a port of Mikeal Rogers's ubiquitous and excellent [request][req] package to the browser.

Jealous of Node.js? Pining for clever callbacks? Request is for you.

Don't care about Node.js? Looking for less tedium and a no-nonsense API? Request is for you too.

browser support

Examples

Fetch a resource:

request('/some/resource.txt', function(er, response, body) {
  if(er)
    throw er;
  console.log("I got: " + body);
})

Send a resource:

request.put({uri:'/some/resource.xml', body:'<foo><bar/></foo>'}, function(er, response) {
  if(er)
    throw new Error("XML PUT failed (" + er + "): HTTP status was " + response.status);
  console.log("Stored the XML");
})

To work with JSON, set options.json to true. Request will set the Content-Type and Accept headers, and handle parsing and serialization.

request({method:'POST', url:'/db', body:'{"relaxed":true}', json:true}, on_response)

function on_response(er, response, body) {
  if(er)
    throw er
  if(result.ok)
    console.log('Server ok, id = ' + result.id)
}

Or, use this shorthand version (pass data into the json option directly):

request({method:'POST', url:'/db', json:{relaxed:true}}, on_response)

Convenient CouchDB

Browser Request provides a CouchDB wrapper. It is the same as the JSON wrapper, however it will indicate an error if the HTTP query was fine, but there was a problem at the database level. The most common example is 409 Conflict.

request.couch({method:'PUT', url:'/db/existing_doc', body:{"will_conflict":"you bet!"}}, function(er, resp, result) {
  if(er.error === 'conflict')
    return console.error("Couch said no: " + er.reason); // Output: Couch said no: Document update conflict.

  if(er)
    throw er;

  console.log("Existing doc stored. This must have been the first run.");
})

See the [Node.js Request README][req] for several more examples. Request intends to maintain feature parity with Node request (except what the browser disallows). If you find a discrepancy, please submit a bug report. Thanks!

Usage

Browserify

Browser Request is a [browserify][browserify]-enabled package.

First, add browser-request to your Node project

$ npm install browser-request

Next, make a module that uses the package.

// example.js - Example front-end (client-side) code using browser-request via browserify
//
var request = require('browser-request')
request('/', function(er, res) {
  if(!er)
    return console.log('browser-request got your root path:\n' + res.body)

  console.log('There was an error, but at least browser-request loaded and ran!')
  throw er
})

To build this for the browser, run it through browserify.

$ browserify --entry example.js --outfile example-built.js

Deploy example-built.js to your web site and use it from your page.

  <script src="example-built.js"></script> <!-- Runs the request, outputs the result to the console -->

License

Browser Request is licensed under the Apache 2.0 license.

More Repositories

1

follow

Very stable, very reliable, NodeJS CouchDB _changes follower
JavaScript
386
star
2

dnsd

Dynamic authoritative name server
JavaScript
269
star
3

bigdecimal.js

Arbitrary-precision Javascript BigInteger and BigDecimal real numbers
Ruby
241
star
4

couchjs

Drop-in replacement JavaScript engine for Apache CouchDB
JavaScript
88
star
5

browserid_couchdb

Mozilla BrowserID support plugin for CouchDB
Erlang
58
star
6

request_jquery

request (NodeJS http client) API using jQuery back-end
JavaScript
56
star
7

manage_couchdb

General CouchDB management tools
JavaScript
54
star
8

traceback

Easy access to the call stack, for Node.js
43
star
9

fastcgi

Simple, robust node.js web server that runs FastCGI applications
JavaScript
38
star
10

procouch

Professional, production CouchDB: auto-view update and cleanup, auto-compact, auto-purge, auto-deploy
JavaScript
15
star
11

erlang-request

The Node.js request API for Erlang
Erlang
13
star
12

static-plus

Build static web sites from templates. Plus.
JavaScript
11
star
13

obj_diff

See (and assert) differences between JS objects, useful for CouchDB validations
JavaScript
11
star
14

stripe_kanso

Kanso and CouchDB support for the Stripe payments API
JavaScript
5
star
15

futon_couchdb

Better Futon
JavaScript
5
star
16

fixed-event

Javascript EventEmitter for one-time tasks
JavaScript
5
star
17

iris-redis

Thin node_redis wrapper to connect to Iris Couch's Redis service
JavaScript
5
star
18

veil

Convert RFC822 and HTTP messages (headers, body) to Javascript objects
JavaScript
4
star
19

query_couchdb

Easy CouchDB queries, similar to Google App Engine
JavaScript
3
star
20

stew

Easy reduce function that does what you want, for Kanso.js
JavaScript
3
star
21

thaijs

thaijs.com site
Shell
2
star
22

browser_bin

Convert an NPM package command-line program into a web page
JavaScript
2
star
23

public_couchdb

Public CouchDB repo
Erlang
1
star
24

voodootikigod-upgrade

Upgrade the minutewith sites
JavaScript
1
star
25

cors_couchdb

Cross-Object Resource Sharing utilities for CouchDB
JavaScript
1
star