# ut-browser-request

> Browser port of the Node.js 'request' package

Latest version **1.0.9** (published 2024-07-04) · Apache 2.0 license · 0 weekly downloads

## Install

```sh
npm install ut-browser-request
pnpm add ut-browser-request
yarn add ut-browser-request
bun add ut-browser-request
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.9 |
| Published | 2024-07-04 |
| First published | 2018-04-13 |
| Weekly downloads | 0 |
| License | Apache 2.0 |
| TypeScript types | none |
| Module format | CommonJS |
| Node | ^14.15.3 \|\| ^16.14.0 |
| Dependencies | 0 |
| Unpacked size | 73.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Jason Smith |
| Maintainers | kalin.krustev |
| Keywords | request, http, browser, ajax |

## Links

- npm: https://www.npmjs.com/package/ut-browser-request
- Repository: https://github.com/softwaregroup-bg/ut-browser-request
- Homepage: http://github.com/softwaregroup-bg/ut-browser-request
- Issues: https://github.com/softwaregroup-bg/ut-browser-request/issues
- npm.io page: https://npm.io/package/ut-browser-request

## Alternatives

- [launchdarkly-js-client-sdk](https://npm.io/package/launchdarkly-js-client-sdk.md) — 2.5M weekly downloads
- [@elastic/elasticsearch](https://npm.io/package/@elastic/elasticsearch.md) — 2.1M weekly downloads
- [@c8y/client](https://npm.io/package/@c8y/client.md) — 15.3K weekly downloads
- [@signaldb/maverickjs](https://npm.io/package/@signaldb/maverickjs.md) — 1.7K weekly downloads
- [@bbc/http-transport-cache](https://npm.io/package/@bbc/http-transport-cache.md) — 1.2K weekly downloads

## Recent versions

- 1.0.9 (latest) — 2024-07-04
- 1.0.8 — 2022-03-11
- 1.0.6 — 2022-03-07
- 1.0.5 — 2021-03-11
- 1.0.3 — 2020-12-03
- 1.0.2 — 2018-04-24
- 1.0.1 — 2018-04-23
- 1.0.0 — 2018-04-13

## README

# 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.

**And this is a fork of [iriscouch/browser-request](https://github.com/iriscouch/browser-request).**

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.

# Examples

Fetch a resource:

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

Send a resource:

```javascript
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.

```javascript
request({
  method:'POST',
  url:'/db',
  body:'{"relaxed":true}',
  json:true
}, on_response)
function on_response(er, response, result) {
  if(er)
    throw er
  if(response.ok)
    console.log('Server ok, id = ' + response.id)
}
```

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

```javascript
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`.

```javascript
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.

This project is not on npm.org. You can still install it via npm:

    $ npm install git+https://github.com/invokemedia/browser-request.git

Next, make a module that uses the package.

```javascript
// 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.

    $ npm run build

Deploy `browser-request.js` to your web site and use it from your page.

```html
  <script src="browser-request.js"></script> <!-- Runs the request, outputs the result to the console -->
```

## Test

You can test this library with `phantomjs`. You just need to run:

    $ npm test

This will run the `test-run.js`, which wraps up `test.js` for the browser/phantom. You will need to make sure `devDependecies` are installed.

## License

Browser Request is licensed under the Apache 2.0 license.

---
_Source: https://npm.io/package/ut-browser-request · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
