8

I'm having quite a hard time with this -- seems like there are a few snippets of code lying around that I can't seem to piece together. I'm simply trying to POST key/value pairs, but getting Connection refused - connect(2) (Errno::ECONNREFUSED). Help!

require 'net/http'
require 'net/https'
require 'uri'

@http = Net::HTTP.new('https://my.url.com/path', 443)
@http.use_ssl = true
@http.start() { |http|
    req = Net::HTTP.post_form(
        URI.parse('https:///my.url.com/path'),
        {'key1' => 'value1', 'key2' => 'value2'}
    )
    req.basic_auth 'username', 'password'
    response = http.request(req)
    puts response.body      
}
hughdbrown
  • 47,733
  • 20
  • 85
  • 108

2 Answers2

20

HTTP#post_form will execute directly, ignoring your other settings. Try this instead:

require 'net/http'
require 'uri'

url = URI.parse('https://my.url.com/path')
req = Net::HTTP::Post.new(url.path)
req.basic_auth 'user', 'pass'
req.use_ssl = true
req.form_data({'key1' => 'val1', 'key2' => 'val2'})

resp = Net::HTTP.new(url.host, url.port).start {|http| http.request(req) }
puts resp

You are likely to run into trouble with the server's certificates. See my other post for instructions on how to get/configure them.

Community
  • 1
  • 1
emboss
  • 38,880
  • 7
  • 101
  • 108
  • Yup, had to do use your other post as well. Is there a way, offhand, to turn off the logging to console? This gets spit out: # `url = URI.parse(@url) req = Net::HTTP::Post.new(url.path) req.basic_auth @username, @password req.set_form_data(postParams, ';') sock = Net::HTTP.new(url.host, 443) sock.use_ssl = true sock.ssl_version='SSLv3' sock.start do |http| response = http.request(req) return response end` –  Aug 01 '11 at 19:44
  • Ah, OK, I see - that's only the output from `puts resp` - try `puts resp.body` instead. – emboss Aug 01 '11 at 19:46
  • Do you think you could help with this ticket? http://stackoverflow.com/questions/6903748/ruby-escaping-parameters-in-set-form-data-post I'm seeing oddities with encoding. –  Aug 01 '11 at 21:12
  • Did. Actually, the ';' was a mistake (c & p, my bad). Additionally I think it's more convenient to use `form_data` instead of `set_form_data` they're both aliases and work the same, but form_data= is just easier on the eyes, I guess. – emboss Aug 01 '11 at 21:42
7

Found this question during my search for a solultion, and just to be clear emboss' code as is is non functional. What I got working is this (inside of a larger Sinatra application):

require 'net/http'
require 'net/https'
require 'uri'

set :api_username, 'usr'
set :api_passwor, 'pswd'

def do_auth_check params
    puts params.inspect

    url = URI.parse("https://my_auth_site.com:443/authenticate")
    req = Net::HTTP::Post.new(url.path)
    req.basic_auth options.api_username, options.api_password
    req.set_form_data({'username' => params[:name], 'password' => params[:pass]})

    sock = Net::HTTP.new(url.host, url.port)
    sock.use_ssl = true
    res = sock.start {|http| http.request(req) }

    # you're on your own down here to identify success/failure, but for me 2xx/3xx was ok and 401/404/500/etc would be failure
    return true if res.code.to_i < 400
    return "Error logging in"
end
Factor Mystic
  • 26,279
  • 16
  • 79
  • 95