将curl调用转换为法拉第请求 [英] converting a curl call to a faraday request

查看:109
本文介绍了将curl调用转换为法拉第请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下卷曲请求-

`curl --socks5 #{proxy} --connect-timeout 10 -H "Accept: application/json" #{url}`

我想写一个法拉第请求而不是curl调用.我正在这样做-

I want to write a faraday request instead of a curl call. I am doing this -

faraday =  Faraday.new("#{url}", ssl:{verify: false} do |f|
    f.proxy "#{proxy}"
end
faraday.get

我收到响应,但响应没有正文或标题.以下是响应-

I am getting a reponse but the response has no body or headers. Following is the response -

#<Faraday::Response:0x0056075d353ad0 @on_complete_callbacks=[], @env=#<Faraday::Env @method=:get @url=#<URI::HTTP:0x0056075d358328 URL:main_url> @request=#<Faraday::RequestOptions proxy=#<Faraday::ProxyOptions uri=#<URI::HTTP:0x0056075dce69d0 URL:proxy_url>>> @request_headers={"User-Agent"=>"Faraday v0.9.2"} @ssl=#<Faraday::SSLOptions (empty)> @response=#<Faraday::Response:0x0056075d353ad0 ...>>>

我在做什么错了?

推荐答案

转换为法拉第最困难的问题是您需要使用SOCKS5代理.法拉第不支持SOCKS代理(有一个为此打开请求请求) .

The hardest issue with the conversion to Faraday is that you need to use a SOCKS5 proxy. Faraday does not support SOCKS proxies (there is an open pull-request for this).

唯一的解决方法是猴子补丁法拉第使用 socksify gem ,它将对SOCKS代理的支持添加到Net::HTTP(默认的法拉第网络适配器). 该过程在此要点 中有很好的描述,我主要复制粘贴此处略有改动.

The only way around this is to monkey-patch Faraday to use the socksify gem which adds support for SOCKS proxies to Net::HTTP (the default Faraday network adapter). The procedure is nicely described in this gist and I mostly copy-paste a slightly altered version of it here.

基本上,您需要执行以下步骤:

Basically you need to follow these steps:

  1. 安装faradaysocksify宝石
  2. Monkey-patch Faraday支持SOCKS.将此代码放入Rails初始化程序中.请注意,仅当您不需要对SOCKS代理进行身份验证(如您的curl命令建议)时,该修补程序才适用.如果您需要代理身份验证,请参见要点以获取支持该更新的补丁程序版本.补丁如下:

  1. Install the faraday and socksify gems
  2. Monkey-patch Faraday to support SOCKS. Put this code into a Rails initializer. Note that the patch only applies if you don't need to authenticate to the SOCKS proxy (as your curl command suggests). If you need proxy authentication, see the gist for a patch version supporting that. The patch is as follows:

class Faraday::Adapter::NetHttp
  def net_http_connection(env)
    if proxy = env[:request][:proxy]
      if proxy[:socks]
        Net::HTTP::SOCKSProxy(proxy[:uri].host, proxy[:uri].port)
      else
        Net::HTTP::Proxy(proxy[:uri].host, proxy[:uri].port, proxy[:uri].user, proxy[:uri].password)
      end
    else
        Net::HTTP
    end.new(env[:url].host, env[:url].port)
  end
end

  • 创建请求.我注意到您可能正在尝试发出HTTPS请求,因此我考虑了这一点,以及curl参数中的超时:

  • Create the request. I noticed you are probably trying to make a HTTPS request so I took this into account, as well as the timeouts you have in the curl parameters:

    PROXY_OPTS = {
            uri:  URI.parse('https://proxy_url:1080'),
            socks: true  
        }
    
    SSL_OPTS = { verify: false }
    
    connection = Faraday.new(url: "https://example.com",
                             ssl: SSL_OPTS,
                             request: { proxy: PROXY_OPTS }) do |faraday|
      faraday.options.timeout = 10           # open/read timeout in seconds
      faraday.options.open_timeout = 10      # connection open timeout in seconds
      faraday.response :logger               # log debug info
      faraday.adapter  :net_http             # use the Net:HTTP adapter
    
      faraday.headers['Accept'] = 'application/json'  # your custom headers
    end
    
    response = connection.get
    response.body
    

  • 最后,请注意忽略对等验证(SSL选项中的verify: false)是不安全的!相反,您应该正确地配置Faraday以使用证书存储来验证对等证书. 此处.

    Finally, please note that ignoring peer verification (verify: false in the SSL options) is insecure! You should instead properly configure Faraday to use a certificate store to verify peer certificates against. This is fully documented here.

    这篇关于将curl调用转换为法拉第请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆