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

倾然丶 夕夏残阳落幕 提交于 2019-11-29 15:21:10

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

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