1

Can someone explain to me why I am getting this error when doing this POST? I pulled the snippet from the Ruby-docs page.

undefined method `hostname' for #URI::HTTP:0x10bd441d8 URL:http://ws.mittthetwitapp.com/ws.phpmywebservice (NoMethodError)

Perhaps I am missing a require or something?

require 'net/http'

uri= URI('http://ws.mywebservice.com/ws.php')
req = Net::HTTP::Post.new(uri.path)
req.set_form_data('xmlPayload' => '<TestRequest><Message>Hi Test</Message></TestRequest>')

res = Net::HTTP.start(uri.hostname, uri.port) do |http|
http.request(req)
end

case res
 when Net::HTTPSuccess, Net::HTTPRedirection
 # OK
else
 res.value
end
6
  • use URI.parse("http://ws.mywebservice.com/ws.php") Commented Nov 17, 2012 at 20:30
  • Thanks. Same error when doing so: Commented Nov 17, 2012 at 20:49
  • Error: res = Net::HTTP.start(uri.hostname, uri.port) do |http| Commented Nov 17, 2012 at 20:50
  • It should be uri.host. Commented Nov 17, 2012 at 21:00
  • changing to uri.host fixed it thanks. please post as the answer so I can give you credit Commented Nov 17, 2012 at 21:16

1 Answer 1

3

If you're using a version of Ruby prior to 1.9.3, you should use uri.host.

URI#hostname was added in Ruby 1.9.3. It is different than URI#host in that it removes brackets from IPv6 hostnames. For non-IPv6 hostnames it should behave identically.

The implementation (from APIdock):

def hostname
  v = self.host
  /\A\[(.*)\]\z/ =~ v ? $1 : v
end
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.