Security News
tea.xyz Spam Plagues npm and RubyGems Package Registries
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
httpreq
Advanced tools
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 843,537 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
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.
Security News
UnitedHealth Group disclosed that the ransomware attack on Change Healthcare compromised protected health information for millions in the U.S., with estimated costs to the company expected to reach $1 billion.