using net module to send POST method request

37 views
Skip to first unread message

Tushar Jain

unread,
Jan 27, 2015, 3:04:50 AM1/27/15
to nod...@googlegroups.com
HI Folks,

I am trying to use NET module, but facing some error
var socket = net.Socket();
socket.on('connect', function () {
    console.log("Connection created");
});
socket.on('data', function (d) {
    console.log(d);
});
socket.on('error', function (err) {
    console.log(err.toString());
});
socket.on('end', function () {
    console.log('connection ends');
});
socket.connect(80,"https://www.google.com");

I am getting getaddrinfo ENOTFOUND error. please let me know if you have any solution.

Also how can i use NET module to send POST method request[I am not allowed to use HTTP module, have to do this using socket programming only]

thanks in advance
Tushar

Aria Stewart

unread,
Jan 27, 2015, 9:35:33 AM1/27/15
to nod...@googlegroups.com

> On Jan 27, 2015, at 3:04 AM, Tushar Jain <mytu...@gmail.com> wrote:
> I am trying to use NET module, but facing some error
> var socket = net.Socket();

[snip]

> socket.connect(80,"https://www.google.com");
>
> I am getting getaddrinfo ENOTFOUND error. please let me know if you have any solution.

That's a URL, not a host name, so it's not looking up -- you want to connect to 'www.google.com' on port 80. (Also, for the record: you're not setting up TLS there, so you'll not be on a secure connection, it won't be https, and if it were, the port for https is 443. Yay complications!)

>
> Also how can i use NET module to send POST method request[I am not allowed to use HTTP module, have to do this using socket programming only]

That's a strange restriction -- homework for a programming course trying to make you understand a simple protocol?

The specification is in the HTTP RFC; in short, it's "POST path HTTP/1.1\r\n\Header: value\r\nHeader: value\r\n\r\nbodyhere", but there's plenty of details.

Aria

Adrien Risser

unread,
Jan 27, 2015, 9:35:49 AM1/27/15
to nod...@googlegroups.com
Wouldn't HTTPS be on port 443 instead of 80?

--
Job board: http://jobs.nodejs.org/
New group rules: https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
---
You received this message because you are subscribed to the Google Groups "nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+un...@googlegroups.com.
To post to this group, send email to nod...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nodejs/0ede7f76-2627-465d-9bb6-31ae4566dcec%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Adrien Risser,
Freelance Node.js Consultant
Reply all
Reply to author
Forward
0 new messages