HTTP request client for JSON APIs
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.
Add it to your package.json file or run in your project folder:
npm install request-json
request = require'request-json';var client = requestcreateClient'';var data =title: 'my title'content: 'my content';clientpost'posts/' datareturn console.logresstatusCode;;clientget'posts/'return console.logbodyrows0title;;data =title: 'my new title';clientput'posts/123/' datareturn console.logresponsestatusCode;;clientdel'posts/123/'return console.logresponsestatusCode;;data =title: 'my patched title';clientpatch'posts/123/' datareturn console.logresponsestatusCode;;
Note: Previously you should call
newClient() but now it is renamed to
You still can use
newClient() though it will throw deprecation message.
data =name: "test";clientsendFile'attachments/' './test.png' dataif errreturn console.logerr;;clientsaveFile'attachments/test.png' './test-get.png'if errreturn console.logerr;;
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.
clientsetBasicAuth'john' 'secret';clientget'private/posts/'return console.logbodyrows0title;;
request-json and request-json-light are downloaded more than 8000 times each month. Here are the companies which use it for their API clients: