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

Discussion in 'Ruby' started by Stuffe L., Apr 13, 2011.

  1. Stuffe L.

    Stuffe L. Guest

    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

    --
    Posted via http://www.ruby-forum.com/.
     
    Stuffe L., Apr 13, 2011
    #1
    1. Advertising

  2. Stuffe L.

    Oscar Sosa Guest

    [Note: parts of this message were removed to make it a legal post.]

    Hi.

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

    On Wed, Apr 13, 2011 at 2:11 PM, Stuffe L. <> wrote:

    > 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
    >
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >



    --
    Oscar Sosa
     
    Oscar Sosa, Apr 14, 2011
    #2
    1. Advertising

  3. Stuffe L.

    7stud -- Guest

    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)
    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

    --
    Posted via http://www.ruby-forum.com/.
     
    7stud --, Apr 14, 2011
    #3
  4. Stuffe L.

    Stuffe L. Guest

    @7stud, I copied the code out of a function and tried to simplify it for
    you guys to read :) 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.

    --
    Posted via http://www.ruby-forum.com/.
     
    Stuffe L., Apr 14, 2011
    #4
  5. Stuffe L.

    7stud -- Guest

    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 :) 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. POST requests do not include data
    in the query string--they put the data in the body of the request. The
    data still has to be in an agreed upon format, though, otherwise the
    other side won't know how to split it up to make sense of the data.

    --
    Posted via http://www.ruby-forum.com/.
     
    7stud --, Apr 14, 2011
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    6
    Views:
    5,585
    Tor Iver Wilhelmsen
    Aug 29, 2005
  2. James
    Replies:
    3
    Views:
    16,483
    Roedy Green
    Nov 25, 2005
  3. otf
    Replies:
    0
    Views:
    593
  4. Ian
    Replies:
    2
    Views:
    2,022
  5. Replies:
    5
    Views:
    457
    Jim Ley
    Apr 23, 2005
Loading...

Share This Page