Socket
Socket
Sign inDemoInstall

httpreq

Package Overview
Dependencies
0
Maintainers
1
Versions
48
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    httpreq

node-httpreq is a node.js library to do HTTP(S) requests the easy way


Version published
Weekly downloads
1M
increased by1.69%
Maintainers
1
Install size
73.2 kB
Created
Weekly downloads
 

Package description

What is httpreq?

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.

What are httpreq's main functionalities?

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);
});

Other packages similar to httpreq

Readme

Source

node-httpreq

node-httpreq is a node.js library to do HTTP(S) requests the easy way

Do GET, POST, upload files, use cookies, change headers, ...

Install

You can install httpreq using the Node Package Manager (npm):

npm install httpreq

How to use


### httpreq.get(url, [options], callback)

Arguments

  • url: The url to connect to. Can be http or https.
  • options: (optional) The following options can be passed:
    • parameters: an object of query parameters
    • headers: an object of headers
    • cookies: an array of cookies
    • binary: true/false (default: false), if true, res.body will a buffer containing the binary data
  • callback(err, res): A callback function which is called when the request is complete. res contains the headers ( res.headers ) and the body ( res.body )

Example without options

var httpreq = require('httpreq');

httpreq.get('http://www.google.com', function (err, res){
	if (err){
		console.log(err);
	}else{
		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);
	}
});

### httpreq.post(url, [options], callback)

Arguments

  • url: The url to connect to. Can be http or https.
  • options: (optional) The following options can be passed:
    • parameters: an object of post parameters ( application/x-www-form-urlencoded is used)
    • headers: an object of headers
    • cookies: an array of cookies
    • binary: true/false (default: false), if true, res.body will a buffer containing the binary data
  • callback(err, res): A callback function which is called when the request is complete. res contains the headers ( res.headers ) and the body ( res.body )

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);
	}
});

### httpreq.uploadFile(options, callback)

Arguments

  • options: The following options can be passed:
    • url: the url to post the files to
    • parameters: an object of post parameters ( multipart/form-data is used)
    • files: an object of files (can be more than one)
    • headers: an object of headers
    • cookies: an array of cookies
    • binary: true/false (default: false), if true, res.body will a buffer containing the binary data
  • callback(err, res): A callback function which is called when the request is complete. res contains the headers ( res.headers ) and the body ( res.body )
var httpreq = require('httpreq');

httpreq.uploadFile({
	url: "http://rekognition.com/demo/do_upload/",
	parameters:{
		name_space	: 'something',
	},
	files:{
		fileToUpload: __dirname + "/exampleupload.jpg"
	}},
function (err, res){
	if (err){
		console.log(err);
	}else{
		console.log(res.body);
	}
});

### httpreq.doRequest(options, callback)

This is used by httpreq.get() and httpreq.post()

Arguments

  • options: The following options can be passed:
    • url: the url to post the files to
    • method: 'GET' or 'POST'
    • parameters: an object of query/post parameters
    • files: an object of files (can be more than one)
    • headers: an object of headers
    • cookies: an array of cookies
    • binary: true/false (default: false), if true, res.body will a buffer containing the binary data
  • callback(err, res): A callback function which is called when the request is complete. res contains the headers ( res.headers ) and the body ( res.body )

Example

var httpreq = require('httpreq');

httpreq.doRequest({
	url: 'https://graph.facebook.com/19292868552',
	method: 'GET',
	parameters: {
		name: 'test'
	}
},
function (err, res){
	if (err){
		console.log(err);
	}else{
		console.log(JSON.parse(res.body));
	}
});

### Downloading a binary file To download a binary file, just add __binary: true__ to the options when doing a get or a post.

Example

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");
        });
    }
});

FAQs

Last updated on 11 Feb 2013

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc