Class: AntiCaptcha::HTTP

Inherits:
Object
  • Object
show all
Defined in:
lib/anti_captcha/http.rb

Overview

AntiCaptcha::HTTP exposes common HTTP routines.

Class Method Summary collapse

Class Method Details

.post_request(options = {}) ⇒ String

Performs a POST HTTP request. Anti Captcha API supports only POST requests.

Parameters:

  • options (Hash) (defaults to: {})

    Options hash.

  • options (String) (defaults to: {})

    url URL to be requested.

  • options (Hash) (defaults to: {})

    payload Data to be sent through the HTTP request.

  • options (Integer) (defaults to: {})

    timeout HTTP open/read timeout in seconds.

Returns:

  • (String)

    Response body of the HTTP request.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/anti_captcha/http.rb', line 18

def self.post_request(options = {})
  uri     = URI(options[:url])
  payload = options[:json_payload] || '{}'
  timeout = options[:timeout] || 60
  headers = { 'User-Agent' => AntiCaptcha::USER_AGENT,
    'Content-Type' => 'application/json' }

  req = Net::HTTP::Post.new(uri.request_uri, headers)
  req.body = payload

  http = Net::HTTP.new(uri.hostname, uri.port)
  http.use_ssl = true if (uri.scheme == 'https')
  http.open_timeout = timeout
  http.read_timeout = timeout
  res = http.request(req)
  res.body

rescue Net::OpenTimeout, Net::ReadTimeout
  raise AntiCaptcha::Error.new('Request timed out.')
end