A simple util to make json requests.
To get started, install the package and start using the library.
npm install simple-json-requester --save
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);
});
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. |
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. |
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. |
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.
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. |
Feel free to make any suggestion to improve this project.
See the list of contributors who participated in this project.
This project is licensed under the MIT License - see the LICENSE.md file for details