如何使HTTParty忽略SSL?

时间:2022-02-05 07:20:10

I'm using a local server to test an application, and make requests to that server from my own machine.

我正在使用本地服务器来测试应用程序,并从我自己的机器向该服务器发出请求。

The test server's SSL is bad, and HTTParty throws errors because of that. From what I read, HTTParty should ignore SSL by default, but when I try to do this:

测试服务器的SSL很糟糕,因此HTTParty会抛出错误。从我读到的内容来看,HTTParty默认应该忽略SSL,但是当我尝试这样做时:

HTTParty.get( "#{ @settings.api_server }#{ url }" ).parsed_response

It throws this error:

抛出这个错误:

OpenSSL::SSL::SSLError at /
SSL_connect returned=1 errno=0 state=SSLv3 read server certificate B: certificate verify failed

How do I make it ignore the SSL?

如何使它忽略SSL?

3 个解决方案

#1


50  

In the latest HTTParty, you can use the verify option to disable SSL verification;

在最新的HTTParty中,您可以使用verify选项来禁用SSL验证;

HTTParty.get( "#{ @settings.api_server }#{ url }", :verify => false ).parsed_response

#2


23  

To make HTTParty always skip SSL cert verification, and not have to specify this in every call:

要使HTTParty始终跳过SSL cert验证,并且不必在每次调用中都指定这一点:

require 'httparty'
HTTParty::Basement.default_options.update(verify: false)

HTTParty.get("#{@settings.api_ssl_server}#{url1}")
HTTParty.get("#{@settings.api_ssl_server}#{url2}")
HTTParty.get("#{@settings.api_ssl_server}#{url3}")
# ...

You can also do this scoped to a class when including HTTParty as a module:

当将HTTParty包含为模块时,还可以将其作用域扩展到类:

require 'httparty'

class Client
  include HTTParty
  default_options.update(verify: false)
end

Client.get("#{@settings.api_ssl_server}#{url1}")
Client.get("#{@settings.api_ssl_server}#{url2}")
Client.get("#{@settings.api_ssl_server}#{url3}")

Or

require 'httparty'

module APIHelpers
  class Client
    include HTTParty
    default_options.update(verify: false)
  end
end
World(APIHelpers)

Client.get("#{@settings.api_ssl_server}#{url1}")
Client.get("#{@settings.api_ssl_server}#{url2}")
Client.get("#{@settings.api_ssl_server}#{url3}")

#3


2  

If you want to still send your certificates, use this flag:

如果您还想发送证书,请使用以下标志:

verify_peer: false

#1


50  

In the latest HTTParty, you can use the verify option to disable SSL verification;

在最新的HTTParty中,您可以使用verify选项来禁用SSL验证;

HTTParty.get( "#{ @settings.api_server }#{ url }", :verify => false ).parsed_response

#2


23  

To make HTTParty always skip SSL cert verification, and not have to specify this in every call:

要使HTTParty始终跳过SSL cert验证,并且不必在每次调用中都指定这一点:

require 'httparty'
HTTParty::Basement.default_options.update(verify: false)

HTTParty.get("#{@settings.api_ssl_server}#{url1}")
HTTParty.get("#{@settings.api_ssl_server}#{url2}")
HTTParty.get("#{@settings.api_ssl_server}#{url3}")
# ...

You can also do this scoped to a class when including HTTParty as a module:

当将HTTParty包含为模块时,还可以将其作用域扩展到类:

require 'httparty'

class Client
  include HTTParty
  default_options.update(verify: false)
end

Client.get("#{@settings.api_ssl_server}#{url1}")
Client.get("#{@settings.api_ssl_server}#{url2}")
Client.get("#{@settings.api_ssl_server}#{url3}")

Or

require 'httparty'

module APIHelpers
  class Client
    include HTTParty
    default_options.update(verify: false)
  end
end
World(APIHelpers)

Client.get("#{@settings.api_ssl_server}#{url1}")
Client.get("#{@settings.api_ssl_server}#{url2}")
Client.get("#{@settings.api_ssl_server}#{url3}")

#3


2  

If you want to still send your certificates, use this flag:

如果您还想发送证书,请使用以下标志:

verify_peer: false