Security News
New Proposed CISA Mandate Would Require Critical Infrastructure to Report Ransom Payments Within 24 Hours
CISA has proposed a set of new rules that would require critical infrastructure to report cyber incidents and ransom payments.
httpreq
Advanced tools
node-httpreq is a node.js library to do HTTP(S) requests the easy way
Weekly downloads
Readme
node-httpreq is a node.js library to do HTTP(S) requests the easy way
Do GET, POST, PUT, PATCH, DELETE, OPTIONS, upload files, use cookies, change headers, ...
This module helps you fight TLS fingerprinting! See the shuffleCiphers
option below.
If you've benefited from this module in any way, please consider donating!
Thank you for your support!
You can install httpreq using the Node Package Manager (npm):
npm install httpreq
var httpreq = require('httpreq');
httpreq.get('http://www.google.com', function (err, res) {
if (err) return console.log(err);
console.log(res.statusCode);
console.log(res.headers);
console.log(res.body);
console.log(res.cookies);
});
Using await/async:
var httpreq = require('httpreq');
var res = await httpreq.get('http://www.google.com');
console.log(res.statusCode);
console.log(res.headers);
console.log(res.body);
console.log(res.cookies);
This module has been updated to support async/await.
In the following examples, simply omit the callback
parameter and prepend it with await
.
Example:
var httpreq = require('httpreq');
var res = await httpreq.post('http://posttestserver.com/post.php', {
parameters: {
name: 'John',
lastname: 'Doe'
}
});
console.log(res.body);
Arguments
username:password
true
if you want to shuffle the TLS ciphers to fight TLS fingerprinting.Example without options
var httpreq = require('httpreq');
httpreq.get('http://www.google.com', function (err, res){
if (err) return console.log(err);
console.log(res.statusCode);
console.log(res.headers);
console.log(res.body);
});
Example with options
var httpreq = require('httpreq');
httpreq.get('http://posttestserver.com/post.php', {
parameters: {
name: 'John',
lastname: 'Doe'
},
headers:{
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:18.0) Gecko/20100101 Firefox/18.0'
},
cookies: [
'token=DGcGUmplWQSjfqEvmu%2BZA%2Fc',
'id=2'
]
}, function (err, res){
if (err){
console.log(err);
}else{
console.log(res.body);
}
});
Arguments
username:password
Example without extra options
var httpreq = require('httpreq');
httpreq.post('http://posttestserver.com/post.php', {
parameters: {
name: 'John',
lastname: 'Doe'
}
}, function (err, res){
if (err){
console.log(err);
}else{
console.log(res.body);
}
});
Example with options
var httpreq = require('httpreq');
httpreq.post('http://posttestserver.com/post.php', {
parameters: {
name: 'John',
lastname: 'Doe'
},
headers:{
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:18.0) Gecko/20100101 Firefox/18.0'
},
cookies: [
'token=DGcGUmplWQSjfqEvmu%2BZA%2Fc',
'id=2'
]
}, function (err, res){
if (err){
console.log(err);
}else{
console.log(res.body);
}
});
Same options as httpreq.post(url, [options], callback)
Same options as httpreq.post(url, [options], callback)
Same options as httpreq.get(url, [options], callback) except for the ability to follow redirects.
You can still use httpreq.uploadFiles({url: 'url', files: {}}, callback)
, but it's easier to just use POST (or PUT):
Example
var httpreq = require('httpreq');
httpreq.post('http://posttestserver.com/upload.php', {
parameters: {
name: 'John',
lastname: 'Doe'
},
files: {
myfile: __dirname + '/file1.jpg',
myotherfile: __dirname + '/file2.jpg'
}
}, function (err, res){
if (err) throw err;
});
Example 2
In case you want to use the same form name for multiple files:
var httpreq = require('httpreq');
httpreq.post('http://posttestserver.com/upload.php', {
parameters: {
name: 'John',
lastname: 'Doe'
},
files: {
myfiles: [__dirname + '/file1.jpg', __dirname + '/file.jpg']
}
}, function (err, res){
if (err) throw err;
});
To download a binary file, just add binary: true to the options when doing a get or a post.
Example
var httpreq = require('httpreq');
httpreq.get('https://ssl.gstatic.com/gb/images/k1_a31af7ac.png', {binary: true}, function (err, res){
if (err){
console.log(err);
}else{
fs.writeFile(__dirname + '/test.png', res.body, function (err) {
if(err)
console.log("error writing file");
});
}
});
To download a file directly to disk, use the download method provided.
Downloading is done using a stream, so the data is not stored in memory and directly saved to file.
Example
var httpreq = require('httpreq');
httpreq.download(
'https://ssl.gstatic.com/gb/images/k1_a31af7ac.png',
__dirname + '/test.png'
, function (err, progress){
if (err) return console.log(err);
console.log(progress);
}, function (err, res){
if (err) return console.log(err);
console.log(res);
});
When specifying the progress
callback (3th parameter), you cannot use async/await.
Use the body option to send a custom body (eg. an xml post)
Example
var httpreq = require('httpreq');
httpreq.post('http://posttestserver.com/post.php',{
body: '<?xml version="1.0" encoding="UTF-8"?>',
headers:{
'Content-Type': 'text/xml',
}},
function (err, res) {
if (err){
console.log(err);
}else{
console.log(res.body);
}
}
);
Example
var httpreq = require('httpreq');
httpreq.post('http://posttestserver.com/post.php', {
proxy: {
host: '10.100.0.126',
port: 8888
}
}, function (err, res){
if (err){
console.log(err);
}else{
console.log(res.body);
}
});
httpreq.doRequest is internally used by httpreq.get() and httpreq.post(). You can use this directly. Everything is stays the same as httpreq.get() or httpreq.post() except that the following options MUST be passed:
Install all depedencies with
npm install
Install mocha with
npm install mocha -g
Run tests:
mocha test/tests.js
Run the async/await tests:
mocha test/tests-async.js
FAQs
node-httpreq is a node.js library to do HTTP(S) requests the easy way
The npm package httpreq receives a total of 778,149 weekly downloads. As such, httpreq popularity was classified as popular.
We found that httpreq demonstrated a healthy version release cadence and project activity because the last version was released less than 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.
Security News
CISA has proposed a set of new rules that would require critical infrastructure to report cyber incidents and ransom payments.
Security News
Redis is no longer OSS, breaking its explicit commitment to remain under the BSD 3-Clause License forever. This has angered contributors who are now working to fork the software.
Product
Socket AI now enables 'AI detected potential malware' alerts by default, ensuring users benefit from AI-powered state-of-the-art malware detection without needing to opt-in.