# How do you post data with a socket?

**URL:** <https://www.ruby-forum.com/t/how-do-you-post-data-with-a-socket/91367>\
**Category:** Ruby\
**Created:** [April 7, 2007, 5:08pm UTC](https://www.ruby-forum.com/t/how-do-you-post-data-with-a-socket/91367 "2007-04-07T17:08:27Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![r3madi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7c8e57/32.png) [@r3madi](https://www.ruby-forum.com/u/r3madi)\
**Post date:** [April 7, 2007, 5:08pm UTC](https://www.ruby-forum.com/t/how-do-you-post-data-with-a-socket/91367/1 "2007-04-07T17:08:27Z")

</div>

I’m trying to make a little program that goes to a page and posts your  
username and password using a ruby socket but I don’t know how to post  
data with a socket. I only know how to get data. I googled and haven’t  
found anything so if anyone has a solution then it would be greatly  
appreciated.

---

<div class="post-metadata">

**Author:** ![r3madi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7c8e57/32.png) [@r3madi](https://www.ruby-forum.com/u/r3madi)\
**Post date:** [April 7, 2007, 5:26pm UTC](https://www.ruby-forum.com/t/how-do-you-post-data-with-a-socket/91367/2 "2007-04-07T17:26:50Z")

</div>

On Sun, Apr 08, 2007 at 12:08:29AM +0900, Hey Y. wrote:

> I’m trying to make a little program that goes to a page and posts your  
> username and password using a ruby socket but I don’t know how to post  
> data with a socket. I only know how to get data. I googled and haven’t  
> found anything so if anyone has a solution then it would be greatly  
> appreciated.

Google for “ruby Net::HTTP”. Or just type “ri Net::HTTP” at the command  
line. After example #4 you’ll see an example that makes a HTTP post.

Of course, if you really want to do this using a plain TCP socket, then  
it’s  
entirely up to you to send a correctly-formatted HTTP POST request and  
to  
parse the response. You will need to read RFC 2616 if you want to do  
that.

If what you really want is to automate interaction with a website, look  
at  
the Mechanize library.

HTH,

Brian.

---

<div class="post-metadata">

**Author:** ![r3madi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7c8e57/32.png) [@r3madi](https://www.ruby-forum.com/u/r3madi)\
**Post date:** [April 7, 2007, 5:37pm UTC](https://www.ruby-forum.com/t/how-do-you-post-data-with-a-socket/91367/3 "2007-04-07T17:37:29Z")

</div>

Brian C. wrote:

> On Sun, Apr 08, 2007 at 12:08:29AM +0900, Hey Y. wrote:
> 
> > I’m trying to make a little program that goes to a page and posts your  
> > username and password using a ruby socket but I don’t know how to post  
> > data with a socket. I only know how to get data. I googled and haven’t  
> > found anything so if anyone has a solution then it would be greatly  
> > appreciated.
> 
> Google for “ruby Net::HTTP”. Or just type “ri Net::HTTP” at the command  
> line. After example #4 you’ll see an example that makes a HTTP post.
> 
> Of course, if you really want to do this using a plain TCP socket, then  
> it’s  
> entirely up to you to send a correctly-formatted HTTP POST request and  
> to  
> parse the response. You will need to read RFC 2616 if you want to do  
> that.
> 
> If what you really want is to automate interaction with a website, look  
> at  
> the Mechanize library.
> 
> HTH,
> 
> Brian.

Thank you for the great response and I know how to post data with  
Net::HTTP and WWW::Mechanize is great but I wanted something more  
challenging and it seems that sockets are faster in speed. Also I’m  
sorry but I don’t really understand what you mean by “send a  
correctly-formatted HTTP POST request and parse the response”. I also  
googled “ruby RFC 2616” and have found this site:  
[http://www.rfc-editor.org/rfc/rfc2616.txt](http://www.rfc-editor.org/rfc/rfc2616.txt). Is the information I’m  
looking for in that website? Sorry for all the questions :(.

---

<div class="post-metadata">

**Author:** ![r3madi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7c8e57/32.png) [@r3madi](https://www.ruby-forum.com/u/r3madi)\
**Post date:** [April 8, 2007, 9:50am UTC](https://www.ruby-forum.com/t/how-do-you-post-data-with-a-socket/91367/4 "2007-04-08T09:50:03Z")

</div>

On Sun, Apr 08, 2007 at 12:37:31AM +0900, Hey Y. wrote:

> Thank you for the great response and I know how to post data with  
> Net::HTTP and WWW::Mechanize is great but I wanted something more  
> challenging and it seems that sockets are faster in speed.

Errm:

1. You want to write a program, in Ruby, which sends HTTP requests down  
a  
socket and parses the responses.

2. Net::HTTP _is_ a program written in Ruby which sends HTTP requests  
down a  
socket and parses the responses.

So why do you think (1) would be faster than (2) ?

B.

---

<div class="post-metadata">

**Author:** ![r3madi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7c8e57/32.png) [@r3madi](https://www.ruby-forum.com/u/r3madi)\
**Post date:** [April 7, 2007, 5:59pm UTC](https://www.ruby-forum.com/t/how-do-you-post-data-with-a-socket/91367/5 "2007-04-07T17:59:02Z")

</div>

Hey Y. wrote:

> Thank you for the great response and I know how to post data with  
> Net::HTTP and WWW::Mechanize is great but I wanted something more  
> challenging and it seems that sockets are faster in speed. Also I’m  
> sorry but I don’t really understand what you mean by “send a  
> correctly-formatted HTTP POST request and parse the response”.

You’ll have to write a packet that conforms to RFC 2616, especially the  
part about “POST”. Then you’ll have to make sense of the response the  
other side will give you.

If you want to work with TCP/IP, and don’t understand the basics of it,  
you won’t get far with your project. Working on the deeper layers of the  
OSI and TCP models _is_ difficult, and requires a lot of reading of the  
respective RFCs.

> I also  
> googled “ruby RFC 2616” and have found this site:  
> [http://www.rfc-editor.org/rfc/rfc2616.txt](http://www.rfc-editor.org/rfc/rfc2616.txt). Is the information I’m  
> looking for in that website? Sorry for all the questions :(.

You haven’t read it, have you? To quote from the abstract:

> The Hypertext Transfer Protocol (HTTP) is an application-level  
> protocol for distributed, collaborative, hypermedia information  
> systems.

–  
Phillip “CynicalRyan” Gawlowski  
[http://cynicalryan.110mb.com/](http://cynicalryan.110mb.com/)

Rule of Open-Source Programming #9:

Give me refactoring or give me death!
