• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

cozy/request-json: Http Client to deal easily with JSON API

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称:

cozy/request-json

开源软件地址:

https://github.com/cozy/request-json

开源编程语言:

CoffeeScript 100.0%

开源软件介绍:

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:




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
sdomino/scribble: A tiny Golang JSON database发布时间:2022-07-09
下一篇:
google/vim-jsonnet: Jsonnet filetype plugin for Vim.发布时间:2022-07-09
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap