Tony Stark
Tony Stark

Reputation: 25558

(ruby) ruby sockets: how to create a POST request?

How does one create a POST request using TCPSocket in Ruby? Is there a special format to making a post? I have the following but I get a parse error (it's for a rails server):

require 'socket'

s = TCPSocket.open("localhost", 3000)
s.puts("POST /<controller>/<action> HTTP/1.1")
s.puts("Host: localhost:3000")
s.puts("Content-Type: application/x-www-form-urlencoded")
s.puts("Content-Length: 103\r\n\r\n")

Upvotes: 2

Views: 1932

Answers (2)

Ken Bloom
Ken Bloom

Reputation: 58800

The Host: field should not include the port number.

Upvotes: 1

Chuck Vose
Chuck Vose

Reputation: 4580

Found this article that may be of some use to you. I especially like Eric Hodel's comment about how to do it with Net::HTTP. I know you specified that you wanted to do TCPSocket.send (presumably because you're working on something slightly more interesting than just sending POSTs), but if you aren't doing something more complicated you may be able to use Net::HTTP and rejoice at how easy it is.

Upvotes: 1

Related Questions