Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get HTTPS response

Tags:

http

ruby

https

This works great:

require 'net/http'

uri = URI('http://api.twitter.com/1/statuses/user_timeline.json')
args = {include_entities: 0, include_rts: 0, screen_name: 'johndoe', count: 2, trim_user: 1}
uri.query = URI.encode_www_form(args)
resp = Net::HTTP.get_response(uri)
puts resp.body

But changing from http to https leads to a meaningless error. I'm not asking why the error is meaningless, I would just like to know what's the closest means of doing get_response for https?

I've seen the 'HTTPS' example in the Net::HTTP doc but it looks not very impressive and will make me manually compose the URL from my parameters hash - no good.

like image 629
Oleg Mikheev Avatar asked Mar 12 '12 14:03

Oleg Mikheev


People also ask

What is HTTP GET response?

GET : The resource has been fetched and transmitted in the message body. HEAD : The representation headers are included in the response without any message body. PUT or POST : The resource describing the result of the action is transmitted in the message body.

How can you get the HTTP status code of a requests response?

The Status-Code element in a server response, is a 3-digit integer where the first digit of the Status-Code defines the class of response and the last two digits do not have any categorization role. There are 5 values for the first digit: S.N. It means the request has been received and the process is continuing.

What is HTTP request and HTTP response?

What is HTTP? The Hypertext Transfer Protocol (HTTP) is designed to enable communications between clients and servers. HTTP works as a request-response protocol between a client and server. Example: A client (browser) sends an HTTP request to the server; then the server returns a response to the client.


1 Answers

Here is a example which works for me under Ruby 1.9.3

require "net/http"

uri = URI.parse("https://api.twitter.com/1/statuses/user_timeline.json")
args = {include_entities: 0, include_rts: 0, screen_name: 'johndoe', count: 2, trim_user: 1}
uri.query = URI.encode_www_form(args)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true

request = Net::HTTP::Get.new(uri.request_uri)

response = http.request(request)
response.body
like image 154
beanie Avatar answered Sep 27 '22 17:09

beanie