Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I POST with the Node.js http client?

Tags:

node.js

I'm trying to follow the documentation here

http://nodejs.org/api/http.html

But I can't find documentation on doing something simple such as POSTing a username and password to a URL. How would I do this?

like image 877
Hoa Avatar asked Jul 23 '26 14:07

Hoa


1 Answers

The node.js documentation isn't particularly clear on this

http://nodejs.org/api/http.html#http_http_request_options_callback

This is how I would make the request, using querystring to parse the input.

//require a few things.

var http = require('http'),
    qs = require('qs');

//These are the post options
var options = {
  hostname: 'www.mysite.com',
  port: 80,
  path: '/auth',
  method: 'POST'
};
//The postdata can be anything, but I'm using querystring 
//to convert it into the format 
//username=User&password=Password to be easily parsed in php

var postdata = qs.stringify({
    username:"User",
    password:"Password"
});

//Initialise the variable that will store the response
var body='';


//Now we're going to set up the request and the callbacks to handle the data
var request = http.request(options, function(response) {
    //When we receive data, we want to store it in a string
    response.on('data', function (chunk) {
        body += chunk;
    });
    //On end of the request, run what we need to
    response.on('end',function() {
        //Do Something with the data
        console.log(body);
    });
});

//Now we need to set up the request itself. 
//This is a simple sample error function
request.on('error', function(e) {
  console.log('problem with request: ' + e.message);
});


//Write our post data to the request
request.write(postdata);
//End the request.
request.end();
like image 160
bolli Avatar answered Jul 25 '26 07:07

bolli



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!