Coder Social home page Coder Social logo

node-request-digest's Introduction

Request digest client in Node.js Build Status

NPM

Perform a client request (http) with a digest authentication, a simple node http digest client.

Disclaimer

Only tested against one server and spec is not followed fully. It works for me and for what I am doing. I often try to improve this npm and I answer to your pull-request and issue ASAP in order to improve your experience with this package. Pay attention, it only works with MD5 algorithm currently.

Usage (callback)

var digestRequest = require('request-digest')('username', 'password');
digestRequest.request({
  host: 'http://test.com',
  path: '/api/v1/test.json',
  port: 80,
  method: 'GET',
  headers: {
    'Custom-Header': 'OneValue',
    'Other-Custom-Header': 'OtherValue'
  }
}, function (error, response, body) {
  if (error) {
    throw error;
  }

  console.log(body);
});

Usage (promise, only >= 1.0.0)

GET example

var digestRequest = require('request-digest')('username', 'password');
digestRequest.requestAsync({
  host: 'http://test.com',
  path: '/api/v1/test.json',
  port: 80,
  method: 'GET',
  excludePort: false,
  headers: {
    'Custom-Header': 'OneValue',
    'Other-Custom-Header': 'OtherValue'
  }
})
.then(function (response) {
  console.log(response.body);
})
.catch(function (error) {
  console.log(error.statusCode);
  console.log(error.body);
});

The digest client will make one request to the server, authentication response is calculated and then the request is made again. Hopefully you will then be authorized.

If you don't want to specify or to use a specific port. Default port value is 80 but if you set the option excludePort to true it won't add a port to your URL.

POST example

Following is a POST with JSON body example.

var digestRequest = require('request-digest')('username', 'password');
digestRequest.requestAsync({
  host: 'http://test.com',
  path: '/api/v1/test',
  port: 80,
  method: 'POST',
  json: true,
  body: {
     myData: 'test'
  },
  headers: {
        'Content-Type': 'application/json'
      }
})
.then(function (response) {
  console.log(response.body);
})
.catch(function (error) {
  console.log(error.statusCode);
  console.log(error.body);
});

Return object

  • Response object :
response = {
  response,
  body
};
  • Error object :
error = {
  response,
  body,
  statusCode
};

Versions:

Pay attention, after version 1.0.0 I have implemented promise support and add a better support of errors with statusCode >= 400.

Contributions

Feel free to contribute and extend this package and if you have bugs or if you want more specs make an issue. Have fun !


Made by Coenen Benjamin with love

node-request-digest's People

Contributors

bnjjj avatar lkiesow avatar meshiest avatar ankitjaininfo avatar mrpossoms avatar zhangyaoxing avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.