SSL Error on HTTP POST (Unknown Protocol)

自古美人都是妖i 提交于 2020-01-12 05:37:05

问题


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


回答1:


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)



回答2:


For me it was because I had started the server as a http (tcp://) servers instead of https (ssl://).

i.e.

bundle exec puma config.ru -b 'tcp://0.0.0.0:3456?key=/path/to/key.key&cert=/path/to/cert.crt'

instead of:

bundle exec puma config.ru -b 'ssl://0.0.0.0:3456?key=/path/to/key.key&cert=/path/to/cert.crt'


来源:https://stackoverflow.com/questions/15843578/ssl-error-on-http-post-unknown-protocol

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!