# request-json

> HTTP request client for JSON APIs

Latest version **0.6.5** (published 2020-05-14) · MIT license · 0 weekly downloads

## Install

```sh
npm install request-json
pnpm add request-json
yarn add request-json
bun add request-json
```

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.6.5 |
| Published | 2020-05-14 |
| First published | 2012-08-29 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | * |
| Dependencies | 2 |
| Unpacked size | 13.1 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| Author | Frank Roussau |
| Maintainers | frankrousseau, mycozycloud |
| Keywords | requests, request, api, http, json, client, simple, utility |

## Links

- npm: https://www.npmjs.com/package/request-json
- Repository: https://github.com/hackervents/request-json
- Homepage: https://github.com/hackerevents/request-json/
- Issues: https://github.com/hackerevents/request-json/issues
- npm.io page: https://npm.io/package/request-json

## Dependencies (2)

- [depd](https://npm.io/package/depd.md) 1.1.2
- [request](https://npm.io/package/request.md) 2.88.0

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 0.6.5 (latest) — 2020-05-14
- 0.6.4 — 2018-11-03
- 0.6.3 — 2018-02-20
- 0.6.2 — 2017-04-27
- 0.6.1 — 2016-08-03
- 0.6.0 — 2016-07-18
- 0.5.6 — 2016-04-26
- 0.5.5 — 2016-01-06
- 0.5.4 — 2015-12-30
- 0.5.3 — 2015-04-25
- 0.5.2 — 2015-02-08
- 0.5.1 — 2015-02-08
- 0.5.0 — 2014-10-31
- 0.4.13 — 2014-10-10
- 0.4.12 — 2014-09-04
- … 33 more at https://npm.io/package/request-json/versions

## README

## About

[Request](https://github.com/mikeal/request) is a great HTTP client for NodeJS,
but if you deal only with JSON, things could be more straightforward. This lib
aims to simplify Request usage for
JSON only requests.

## Install

Add it to your package.json file or run in your project folder:

    npm install request-json --save

## Build status

[![Build
Status](https://travis-ci.org/cozy/request-json.png?branch=master)](https://travis-ci.org/cozy/request-json)

## How it works

```javascript
request = require('request-json');
var client = request.createClient('http://localhost:8888/');

var data = {
  title: 'my title',
  content: 'my content'
};
client.post('posts/', data, function(err, res, body) {
  return console.log(res.statusCode);
});

client.get('posts/', function(err, res, body) {
  return console.log(body.rows[0].title);
});

data = {
  title: 'my new title'
};
client.put('posts/123/', data, function(err, res, body) {
  return console.log(res.statusCode);
});

client.del('posts/123/', function(err, res, body) {
  return console.log(res.statusCode);
});

data = {
  title: 'my patched title'
};
client.patch('posts/123/', data, function(err, res, body) {
  return console.log(res.statusCode);
});
```

**Note:** Previously you should call `newClient()` but now it is renamed to `createClient()`.
You still can use `newClient()` though it will throw deprecation message.

### Extra: files

```javascript
data = {
  name: "test"
};
client.sendFile('attachments/', './test.png', data, function(err, res, body) {
  if (err) {
    return console.log(err);
  }
});

client.saveFile('attachments/test.png', './test-get.png', function(err, res, body) {
  if (err) {
    return console.log(err);
  }
});

```

`sendFile` can support file path, stream, array of file path and array of
streams. Each file is stored with the key 'file + index' (file0, file1,
file2...) in the request in case of array. For a single value, it is stored in
the field with key 'file'.
If you use a stream, it must have a "path" attribute containing its path or filename.


### Extra: basic authentication

```javascript
client.setBasicAuth('john', 'secret');
client.get('private/posts/', function(err, res, body) {
  return console.log(body.rows[0].title);
});

```

### Extra: headers manipulation

```javascript
client.headers['Cookie'] = 'Your cookie';
```

### Extra: promises

```javascript
client.get('posts/')
  .then(function(result) {
    console.log(result.res.statusCode);
    console.log(result.body); 
  }).catch(err) {
    console.log(err);
  }
});
```



## Who uses it

request-json and request-json-light are downloaded more than 50 000 times each
month. Here are the companies which use it for their API clients:

* [Cozy Cloud](http://cozy.io) Personal Cloud Solutions
* [LeanKit](http://leankit.com/) Team Collaboration Solutions
* [Forecast](http://forecast.io/) Weather Forecasting
* [JS Report](http://jsreport.net/) Reporting Platform
* [Clearhead](http://clearhead.me/) Digital Optimization Agency

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