react-http-client

1.0.1 • Public • Published

HTTP Client for React Apps

Installing

npm install react-http-client

Uses

The handler takes 3 args

  1. URL
  2. Body : default {}
  3. Headers : default {}
import httpClient from 'react-http-client';
 
// or
 
const httpHandler = require('react-http-client');
 
const postResponse = await httpHandler.post(
  // url
  'http://dummy.restapiexample.com/api/v1/create',
  // body
  {
    name: 'test',
    salary: '123',
    age: '23',
  }
  // headers if any
);
 
console.log(postResponse);
// {
//   status: 'success',
//   data: { name: 'test', salary: '123', age: '23', id: 66 }
// }
 
const getResponse = await httpHandler.get(
  'http://dummy.restapiexample.com/api/v1/employees'
);
 
console.log(getResponse);
 
// {
//   status: 'success',
//   data: [
//     {
//       id: '1',
//       employee_name: 'Tiger Nixon',
//       employee_salary: '320800',
//       employee_age: '61',
//       profile_image: ''
//     },
//     {
//       id: '2',
//       employee_name: 'Garrett Winters',
//       employee_salary: '170750',
//       employee_age: '63',
//       profile_image: ''
//     },
//     {
//       id: '3',
//       employee_name: 'Ashton Cox',
//       employee_salary: '86000',
//       employee_age: '66',
//       profile_image: ''
//     },
//      ...
//      ...
//   ]
// }

You can also use inbuilt fetch to do the same but this will provide you a simple interface with which you can make calls easily.

-- Thanks

Package Sidebar

Install

npm i react-http-client

Weekly Downloads

41

Version

1.0.1

License

ISC

Unpacked Size

2.49 kB

Total Files

3

Last publish

Collaborators

  • cksharma11