Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
basic-request
Advanced tools
Basic request to a URL, send to a function(error, body), following redirects
A very basic library to send an HTTP request to a URL and get a result.
There are approximately 10000 request packages in npm. This one adds no extra dependencies,
and uses the node.js convention of function(error, result)
without any added parameters.
No longer will you need to check the status code of the response; basic-request treats all
status codes other than 200 as errors. The library can work with promises too.
As an extra, basic-request follows redirects.
Install with npm:
npm install basic-request
Versions 2 and later should be used at least with Node.js v8 or later:
In your code just require the package:
var request = require('basic-request');
To make a regular GET request use await request.get()
:
const body = await request.get('http://httpbin.org/')
console.log('Received %s', body);
That is it! No wading through responses, parsing status codes or anything else; basic-request will do that for you. Any errors are thrown as exceptions.
You can also use a callback in the traditional node.js fashion:
request.get('http://httpbin.org/', function(error, body) {
if (error) {
return console.error('Could not access httpbin: %s', error);
}
console.log('Received %s', body);
});
It even follows redirects!
You can see a couple of examples in the test file.
The basic-request package supports PUT, POST, PATCH and DELETE methods, and will even stringify objects into JSON for you:
try {
const body = await request.post('http://httpbin.org/', {attribute: 'value'})
console.log('Received %s', body);
} catch (exception) {
return console.error('Could not access httpbin with POST: %s', error);
}
Likewise with request.put()
, request.patch()
, request.delete()
.
Again, can be used with a traditional callback as last parameter:
request.post('http://httpbin.org/', {attribute: 'value'}, function(error, body) {
if (error) {
return console.error('Could not access httpbin with POST: %s', error);
}
console.log('Received %s', body);
});
A couple of additional features have crept in, using a second (optional) parameter params
:
request.get(url, params, callback);
For post and put, it's a third (optional) parameter params
:
request.post(url, body, params, callback);
request.put(url, body, params, callback);
Pass a retries
param to retry requests a number of times:
request.get('http://httpbin.org/', {retries: 2}, function(error, body) {
[...]
});
Pass a timeout
param to abort the query after the given number of milliseconds:
request.get('http://httpbin.org/', {timeout: 1000}, function(error, body) {
[...]
});
Pass a headers
param object to send each key as a header:
var headers = {
'content-type': 'application/x-www-form-urlencoded; charset=UTF-8',
};
request.post('http://httpbin.org/', {a: 5}, {headers: headers}, function(error, body) {
[...]
});
Pass an agent
object to use for keep alive:
request.get('http://httpbin.org/', {agent: new AgentKeepAlive()}, function(error, body) {
[...]
});
By default, no agent is used. Consider using agentkeepalive if you need persistent connections, instead of the default agent.
If params.buffer
is truthy, then a raw buffer is returned
instead of converting to string first.
When the server response has content-type application/json
then it will be parsed and returned as an object instead of a string:
const result = await request.get('http://httpbin.org/json')
console.log(`Type ${typeof result}`) //-> 'object'
getResponse()
In case you want to get the response stream and parse it yourself,
basic-request since 1.2.0 supports getResponse()
:
request.getResponse(url, method, body, params, callback);
where method
can be any HTTP valid method: GET
, POST
...
and the callback
will have the signature function(error, response)
.
The remaining parameters are as explained above.
body
and params
are still optional.
Example:
request.getResponse('http://httpbin.org/post', POST, {a: 5}, function(error, response) {
if (error) return console.error('Could not post: %s', error);
response.pipe(output);
});
getParsed()
When want to need to have more visibility into the response
you can use getParsed()
(since 3.0.0):
const parsed = await request.getParsed(url, method, body, params);
Parameters are the same as in request.getResponse()
,
except that it does not accept a callback:
it is always promise-based.
The returned parsed object will have the following attributes:
status
: HTTP status code.headers
: object with headers.buffer
: unparsed output buffer.body
: parsed body / JSON object.Example:
const parsed = await request.getParsed('http://httpbin.org/post', POST, {a: 5})
console.log(`Status ${parsed.status}, type ${parsed.headers['content-type']})
console.log(`body: ${parsed.body}`)
(The MIT License)
Copyright (c) 2013-2020 Alex Fernández alexfernandeznpm@gmail.com
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
FAQs
Basic request to a URL, send to a function(error, body), following redirects
The npm package basic-request receives a total of 676 weekly downloads. As such, basic-request popularity was classified as not popular.
We found that basic-request demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
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.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.