Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

request-json

Package Overview
Dependencies
Maintainers
2
Versions
48
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

request-json

HTTP request client for JSON APIs

  • 0.6.4
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
516
decreased by-85.3%
Maintainers
2
Weekly downloads
 
Created
Source

About

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

How it works

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

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

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

Extra: headers manipulation

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

Extra: promises

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:

Keywords

FAQs

Package last updated on 03 Nov 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc