Git Product home page Git Product logo

simple-json-requester's Introduction

Simple JSON Requester

A simple util to make json requests.

Getting Started

To get started, install the package and start using the library.

  npm install simple-json-requester --save

Example

const requester = require('simple-json-requester');

// A simple get request
requester.get(
  'http://localhost:1234/article',
  {
    title: 'Foo'
  }
).then(function(data) {
  console.log(data);
});

// A simple post request
requester.post(
  'http://localhost:1234/article/create',
  {
    title: 'Foo'
    content: 'Bar!'
  }
).then(function(data) {
  console.log(data);
});

// A simple put request
requester.put(
  'http://localhost:1234/article/update',
  {
    title: 'Foo'
    content: 'Bar!'
  }
).then(function(data) {
  console.log(data);
});

// A simple delete request
requester.delete('http://localhost:1234/article/1/remove').then(function(data) {
  console.log(data);
});

Methods

get

Makes a GET request to an especified endpoint.

Attribute Type Required Description
url string Yes Endpoint's url
data object No Request data. This data is sent via the url params.
config object No Configuration object. To setup extra things such as cors, and headers.

post

Makes a POST request to an especified endpoint.

Attribute Type Required Description
url string Yes Endpoint's url
data object No Request data. This data is sent in the body.
config object No Configuration object. To setup extra things such as cors, and headers.

put

Makes a PUT request to an especified endpoint.

Attribute Type Required Description
url string Yes Endpoint's url
data object No Request data. This data is sent in the body.
config object No Configuration object. To setup extra things such as cors, and headers.

delete

Makes a DELETE request to an especified endpoint.

Attribute Type Required Description
url string Yes Endpoint's url
data object No Request data. This data is sent in the body.
config object No Configuration object. To setup extra things such as cors, and headers.

All of these methods return a promise that resolves in data in JSON format.

Configuration

As a third parameter for each of the previous described methods a configuration object can be passed. In the next table the available options are described.

Attribute Type Description
cors boolean to specify if it is a CORS request.
extraHeaders object extra headers to be sent in the request.
useDefaultHeaders boolean to specify if the default headers used. The default headers contain 'Accept' and 'Content-Type' for JSON.

Contributing

Feel free to make any suggestion to improve this project.

Authors

See the list of contributors who participated in this project.

License

This project is licensed under the MIT License - see the LICENSE.md file for details

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.