【问题标题】:Httparty request to curlHttpparty 请求卷曲
【发布时间】:2016-03-29 09:03:05
【问题描述】:

有没有办法将 httparty 请求转换为 curl 请求?我希望能够以 curl 格式向其他开发人员发送我失败的请求。

我在想这样的事情:

web_caller = HTTPartyClass.new
web_caller.hit_api
web_caller.last_request_as_curl

last_request_as_curl 会将先前的请求转换为 curl 字符串,我可以在调试时将其提供给 API 的所有者。

理想情况下,last_request_as_curl 会返回类似:“curl -X POST .....”

【问题讨论】:

  • 你能展示一下产生你想要明确的请求的代码吗?
  • 最初的问题非常开放。我以一些精度更新了问题并添加了我的解决方案。如果有人遇到更好的解决方案,请分享我们现有的解决方案。

标签: ruby curl httparty


【解决方案1】:

我在 HTTParty 基类中写了一个方法:

def capture_curl(request)
  options = request.options
  method  = request.http_method.to_s.split(':').last.upcase
  curl_str =  "curl -k -X #{method} "
  if(options[:basic_auth].present?)
    curl_str += "-u #{options[:basic_auth].values.join(':')} "
  end
  curl_str +=
    "-d " +
    options[:body]
      .merge(options[:default_params])
      .map{|a,k| "#{a}=#{k}"}
      .join('&') + " "
  curl_str += "#{options[:base_uri]}#{request.path.path}"
  @curl_str = curl_str
end

def last_curl
  @curl_str
end

我有一个始终处理响应对象的方法,在该方法中我将response.request 传递给capture_curl。

【讨论】:

    猜你喜欢
    • 2018-09-20
    • 2019-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-25
    • 2021-05-31
    • 2011-04-23
    • 2019-03-31
    相关资源
    最近更新 更多