Coder Social home page Coder Social logo

colindcli / http-post Goto Github PK

View Code? Open in Web Editor NEW

This project forked from samt/http-post

0.0 2.0 0.0 116 KB

This utility extends the functionality of the 'http' library in stock node.js. It returns a post request function in a very similar way to node's http.get(). It supports both normal urlencoded payloads and multipart/form-data.

License: MIT License

http-post's Introduction

http-post

This utility extends the functionality of the http library in stock node.js providing a post request function in the same fashion of node's http.get().

In the same style as http.get(), this function calls req.end() automatically

Installing

npm install http-post

Usage and parameters

http-post(options, data[, files[, callback]])

or

http-post(options, data[, callback])

options

Options are the same as the ones for http.request() except method will always be forced to POST. Note that options can be replaced with the full URI of the request similar to http.get allowing for even greater flexibility in your post requests.

data

Data should be key/value pairs of form data. This does not handle file data, see the files option below for more information on uploading files.

var data = {
	name: "Sam",
	email: "[email protected]",
	gender: "m",
	languages: [
		"C",
		"C++",
		"Java",
		"JavaScript",
		"PHP",
		"Python"
	]
}

Pass it an empty array if you do not need to send any form data.

files

This param is another JavaScript object that can contain many files to be posted

var files = [
	{
		param: "img",
		path: "./assets/mycoolimage.png"
	},
	{
		param: "somefile",
		name: "mydata.txt",
		path: "C:\\Users\\Sam\\Documents\\asdf.txt"
	}
]

You may chose to specify an optional name in your array. It will override the file name as it exists in the filesystem and name it the name you specified for the request.

callback

Callback is the same from http.request(). It accepts an instance of http.ClientResponce that has been created during the time of the request.

Return

Returns an instance of http.ClientRequest

Examples

Setting up

var http = require('http');
http.post = require('http-post');

Posting data

http.post('http://localhost/postscript.php', { name: 'Sam', email: '[email protected]' }, function(res){
	response.setEncoding('utf8');
	res.on('data', function(chunk) {
		console.log(chunk);
	});
});

Posting a file

var files = {
	{
		param: "file",
		path: "./assets/img/something.png"
	}
};

http.post('http://localhost/postscript.php', [], files, function(res){
	//...
});

Posting multiple files

var files = {
	{
		param: "file",
		path: "./assets/img/something.png"
	},
	{
		param: "junk",
		path: "/home/sam/hello.txt"
	}
};

http.post('http://localhost/postscript.php', [], files, function(res){
	// ...
});

Posting data and files

var data = {
	name: 'Sam',
	drink: 'coffee'
};

var files = {
	{
		param: "file",
		path: "./assets/img/something.png"
	},
	{
		param: "junk",
		path: "/home/sam/hello.txt"
	}
};

http.post('http://localhost/postscript.php', data, files, function(res){
	// ...
});

License

The MIT License

http-post's People

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.