HTTP request, with headers, post data and SSL :(

Hello everyone

I am new to ruby and have been struggeling with this http api thing. I
need to do a request with certian request headers, certain post
variables and SSL.
I got it to work with SSL and the headers, but I just cant get it to
work when i try to cenvert it to post. Very frustrating.

Heres how far i got (this is without post, which i couldnt get to work):

post_me = ‘xml data’

headers = {‘Header’ => ‘Whatever’}
uri = URI.parse(“https://example.com”)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
response = http.post(uri.request_uri, get_string, headers)

Any help very much apreciated

Hi.

You can try Mechanize, it works for me like a charm.

Stuffe L. wrote in post #992584:

Hello everyone

I am new to ruby and have been struggeling with this http api thing. I
need to do a request with certian request headers, certain post
variables and SSL.
I got it to work with SSL and the headers, but I just cant get it to
work when i try to cenvert it to post. Very frustrating.

Heres how far i got (this is without post, which i couldnt get to work):

post_me = ‘xml data’

headers = {‘Header’ => ‘Whatever’}
uri = URI.parse(“https://example.com”)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
response = http.post(uri.request_uri, get_string, headers)

Any help very much apreciated

  1. In that code, get_string is undefined.
  2. In that code, there are no require statements requiring the necessary
    libraries.

See if you can get this to work:

require ‘net/http’
require ‘net/https’
require ‘uri’

url = URI.parse(‘https://www.some_host.com/some_path_to/page.htm’)

http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true

data = “a=1&b=2&c=3”

headers = {
‘Cookie’ => cookie,
‘Referer’ => ‘http://profil.wp.pl/login.html’,
‘Content-Type’ => ‘application/x-www-form-urlencoded’
}

resp = http.post(url.path, data, headers)
puts resp.body

And you might want to read this:

http://www.rubyinside.com/how-to-cure-nethttps-risky-default-https-behavior-4010.html

Stuffe L. wrote in post #992713:

@7stud, I copied the code out of a function and tried to simplify it for
you guys to read :slight_smile: But I must have forgot what you said.
But if you do as in your example, the contents of the data variable will
be send as a part of the query string no?

No. This line:

resp = http.post(url.path, data, headers)

tells ruby to send a post request. A POST request does not add a
query string to the url, rather a POST request inserts the data in the
body of the request.

If the format of the data is confusing you because it looks like a query
string, the data in the body of the request still has to be in
a mutually agreed upon format–otherwise the server won’t know how to
split it up to make sense of the data. The query string format is the
standard format for data sent in post requests, too. If your server is
expecting the data in some other format, then you can format the data
appropriately.

@7stud, I copied the code out of a function and tried to simplify it for
you guys to read :slight_smile: But I must have forgot what you said.
But if you do as in your example, the contents of the data variable will
be send as a part of the query string no? Therefore it will still a get
and I know that works sometimes, but in this particular case it MUST be
post!

@Oscar Will, do later today! Thanks.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs