Socket
Socket
Sign inDemoInstall

service-request-client

Package Overview
Dependencies
94
Maintainers
2
Versions
7
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    service-request-client

An extension to request to facilitate making requests against discoverable HTTP services


Version published
Weekly downloads
7
Maintainers
2
Created
Weekly downloads
 

Readme

Source

Service Request Client

Utility module for making HTTP requests against discoverable HTTP services.

Supported service discovery technologies include:

  • Container DNS
  • ZooKeeper (to be added in the future)
  • Consul (to be added in the future)
  • Basic Host Port

NPM

Basic Usage

The following is the most basic example of a Container DNS client:

'use strict';

const Client = require('service-request-client').ContainerDNSClient;

// Instantiate a client
const client = new Client(
  'service-name', 
  8001, 
  'my/service/path/v1', 
  { 
    verbose: true, 
    retries: 2,
    timeoutMs: 3000, 
    correlationHeaderName: 'X-Unity-CorrelationID'
  }
);

const query = {
  "myParam": "myValue"  
};

const headers = {
  "Content-Type": "application/json"  
};

const body = {
 "prop1": "value1",
 "prop2": "value2"  
};

// Invoke a GET request against the service endpoint (promise style)
client.get('item/search', { query, headers })
  .then(res => console.log(res.body))
  .catch(err => console.log(err));

// Invoke a GET request against the service endpoint (async/await style)
let res;
  
try {
  res = await client.get('item/search', { query, headers });
  console.log(res.body);
} catch (err) {
  console.log(err);
}

// Invoke a PUT request against the service
client.put('item', { query, headers }, body)
  .then(res => console.log(res.body))
  .catch(err => console.log(err));

// Invoke a POST request against the service
client.post('item', { query, headers }, body)
  .then(res => console.log(res.body))
  .catch(err => console.log(err));  

// Invoke a DELETE request against the service
client.delete('item', { query, headers }, body)
  .then(res => console.log(res.body))
  .catch(err => console.log(err));

 // Invoke a GET request against the service
 client.method('GET', 'item/search', { query, headers }, body)
   .then(res => console.log(res.body))
   .catch(err => console.log(err));  

The following is the most basic example of a Host Port client:

'use strict';

const Client = require('service-request-client').HostPortClient;

// Instantiate a client
const client = new Client(
  'api.test.io', 
  80, 
  'v1', 
  { 
    verbose: true, 
    retries: 2,
    timeoutMs: 3000
  }
);

const query = {
  "myParam": "myValue"  
};

const headers = {
  "Content-Type": "application/json"  
};

const body = {
 "prop1": "value1",
 "prop2": "value2"  
};

// Invoke a GET request against the service endpoint (promise style)
client.get('item/search', { query, headers })
  .then(res => console.log(res.body))
  .catch(err => console.log(err));

// Invoke a GET request against the service endpoint (async/await style)
let res;
  
try {
  res = await client.get('item/search', { query, headers });
  console.log(res.body);
} catch (err) {
  console.log(err);
}

// Invoke a PUT request against the service
client.put('item', { query, headers }, body)
  .then(res => console.log(res.body))
  .catch(err => console.log(err));

// Invoke a POST request against the service
client.post('item', { query, headers }, body)
  .then(res => console.log(res.body))
  .catch(err => console.log(err));  

// Invoke a DELETE request against the service
client.delete('item', { query, headers }, body)
  .then(res => console.log(res.body))
  .catch(err => console.log(err));

 // Invoke a GET request against the service
 client.method('GET', 'item/search', { query, headers }, body)
   .then(res => console.log(res.body))
   .catch(err => console.log(err));  

FAQs

Last updated on 26 Feb 2018

Did you know?

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc