Errno::ECONNRESET: Connection reset by peer in Rails using rest-client

后端 未结 2 416
-上瘾入骨i
-上瘾入骨i 2020-12-21 03:53

We have a Ruby on Rails application and this has a \"search\" functionality (search for some company). From browser user key-in some name and hit search and this search make

相关标签:
2条回答
  • 2020-12-21 04:02

    Try to the following

    resp_data = RestClient::Request.new(
        method: :get,
        url: @request_url, #=> https://api.example.com/auth2/endpoint
        :headers => {
            :Authorization => @header, #=> "Bearer access_token",
        }
    )
    
    rescue => error
        puts 'Exception: ' error.message
    
    0 讨论(0)
  • 2020-12-21 04:21

    It's very wired. I met the same problem.

    my script is shown as below: ( works great in my local machine, works greate in remote server, until someday the disk was full and this scripts dead, saying: Errno::ECONNRESET: Connection reset by peer)

    ENV['RAILS_ENV'] = ARGV.first || ENV['RAILS_ENV'] || 'production'
    require File.expand_path(File.dirname(__FILE__) + "/../config/environment")
    require 'rails'
    require 'rubygems'
    
    require 'rest-client'
    
    url = 'https://api-cn.faceplusplus.com/cardpp/v1/ocridcard'
    id_card = File.expand_path(File.dirname(__FILE__) + "/id_card.jpg")
    
    puts "== id_card: #{id_card}"
    response = RestClient.post url,
      {
        api_key: 'Td_XijYBZUMp',
        api_secret: 'ihehEuWYwOWM',
        image_file: File.open(id_card, 'rb')
      }
    
    puts "==response: "
    puts response.inspect
    

    my environment: ruby 2.5.0 , ubuntu server 16, with 8 core CPU, 50G ram

    this problem happens when I found my hard disk was used 100%. no space left.

    However, once I freed enough disk space, this problem exists.

    after I restart my server, this problem exists.

    when I run this script under rails, this problem exists.

    However, when I run this script stand alone, it works fine.

    so , finally, I turned to "curl" command. works great!

    the working CURL script looks like:

    $ curl -X POST "https://api-cn.faceplusplus.com/cardpp/v1/ocridcard" \ 
    -F "api_key=Td_XijYBCOYh-Rf_kCMj" \
    -F "api_secret=iheWYoQcbCPM9n2VS" \
    -F "image_file=@scripts/id_card.jpg
    
    0 讨论(0)
提交回复
热议问题