Security News
RubyGems.org Adds New Maintainer Role
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
The httpreq npm package is a module that provides a simple interface for making HTTP requests in Node.js. It is designed to make HTTP calls straightforward by handling the complexities of Node's native http module.
GET requests
This feature allows you to perform HTTP GET requests to retrieve data from a specified URL.
httpreq.get('http://example.com', function (err, res){
if (err) return console.error(err);
console.log(res.body);
});
POST requests
This feature enables you to send HTTP POST requests with parameters to a specified URL, which can be used for submitting form data or interacting with REST APIs.
httpreq.post('http://example.com', {
parameters: {
key1: 'value1',
key2: 'value2'
}
}, function (err, res){
if (err) return console.error(err);
console.log(res.body);
});
Custom headers
This feature allows you to set custom headers for your HTTP requests, which can be necessary for APIs that require specific headers for authentication or content negotiation.
httpreq.get('http://example.com', {
headers: {
'User-Agent': 'My Custom User Agent'
}
}, function (err, res){
if (err) return console.error(err);
console.log(res.headers);
});
File uploads
This feature is used to upload files to a server. It supports multipart form data, which is commonly used for file uploads in web forms.
httpreq.post('http://example.com/upload', {
files: {
file1: '/path/to/file1.txt',
file2: '/path/to/file2.jpg'
}
}, function (err, res){
if (err) return console.error(err);
console.log(res.body);
});
Axios is a popular HTTP client for the browser and Node.js. It provides a promise-based API and has a wide range of features including interceptors, automatic transforms for JSON data, and client-side protection against XSRF. It is often considered more feature-rich and modern compared to httpreq.
Request is a simplified HTTP request client that was very popular in the Node.js community but has been deprecated. It offered a simple interface for making all types of HTTP requests and supported features like OAuth signing, form uploads, and cookies. Despite its deprecation, it is still used for comparison due to its historical significance.
Node-fetch is a light-weight module that brings the Fetch API to Node.js. It is a window.fetch polyfill that aims to stay consistent with the browser's implementation of fetch. It is promise-based and is a good choice for users who prefer the Fetch API's approach to making HTTP requests.
Got is a human-friendly and powerful HTTP request library for Node.js. It provides a lot of features out of the box like retries, streams, and pagination, and has a more modern API using promises and async/await. It is considered a good alternative to httpreq with more advanced capabilities.
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, ...
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);
});
Arguments
username:password
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 + "/testupload.jpg",
myotherfile: __dirname + "/testupload.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);
});
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:
If you like this module or you want me to update it faster, feel free to donate. It helps increasing my dedication to fixing bugs :-)
FAQs
node-httpreq is a node.js library to do HTTP(S) requests the easy way
The npm package httpreq receives a total of 430,384 weekly downloads. As such, httpreq popularity was classified as popular.
We found that httpreq 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.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.
Security News
Research
Socket's threat research team has detected five malicious npm packages targeting Roblox developers, deploying malware to steal credentials and personal data.