Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SSL Error on HTTP POST (Unknown Protocol)

Tags:

ruby

https

ssl

api

Trying to connect to Imgur API via SSL gives me an error. Here's the code and the error:

  API_URI = URI.parse('https://api.imgur.com')
  API_PUBLIC_KEY = 'Client-ID --'

  ENDPOINTS = {
    :image   => '/3/image',
    :gallery => '/3/gallery'
  }

  # Public: Upload an image
  #
  # args    - The image path for the image to upload
  # 
  def upload(image_path)
    http = Net::HTTP.new(API_URI.host)
    http.use_ssl = true
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE

    params   = {'image' => File.open(image_path)}
    request  = Net::HTTP::Post.new(API_URI.request_uri)
    request.set_form_data(params)
    request.add_field('Authorization', API_PUBLIC_KEY)

    response = http.request(request)
    puts response.body
  end

And the error:

`connect': SSL_connect returned=1 errno=0 state=SSLv2/v3 read server hello A: unknown protocol (OpenSSL::SSL::SSLError)

I know VERIFY_NODE is not good practice but I just want to test the connection for now.

Ruby version: 1.9.2

like image 435
Chris911 Avatar asked Apr 05 '13 21:04

Chris911


1 Answers

Specifying the port when creating the HTTP client fixed this problem.

http = Net::HTTP.new(API_URI.host, API_URI.port)

or

http = Net::HTTP.new(API_URI.host, 443)
like image 157
Chris911 Avatar answered Oct 19 '22 05:10

Chris911