简体   繁体   English

使用Rails运行HTTP请求

[英]Running a HTTP request with rails

It has been a while since I have used Rails. 自从我使用Rails已经有一段时间了。 I currently have a curl request as follows 我目前有一个卷曲请求,如下

curl -X GET -H 'Authorization: Element TOKEN, User TOKEN' 'https://api.cloud-elements.com/elements/api-v2/hubs/marketing/ping' 

All I am looking to do is to be able to run this request from inside of a rails controller, but my lack of understanding when it comes to HTTP requests is preventing me from figuring it out to how best handle this. 我要做的就是能够从Rails控制器内部运行此请求,但是由于对HTTP请求的理解不足,这使我无法确定如何最好地处理该请求。 Thanks in advance. 提前致谢。

Use this method for HTTP requests: 对HTTP请求使用此方法:

  def api_request(type , url, body=nil, header =nil )
    require "net/http"
    uri = URI.parse(url)
    case type
    when :post
      request = Net::HTTP::Post.new(uri)
      request.body = body
    when :get
      request = Net::HTTP::Get.new(uri)
    when :put
      request = Net::HTTP::Put.new(uri)
      request.body = body
    when :delete
      request = Net::HTTP::Delete.new(uri)
    end
    request.initialize_http_header(header)
    #request.content_type = 'application/json'
    response = Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https') {|http| http.request request}
  end

Your example will be: 您的示例将是:

api_request(:get, "https://api.cloud-elements.com/elements/api-v2/hubs/marketing/ping",nil, {"Authorization" => "Element TOKEN, User TOKEN" })

It would be something like the following. 就像下面这样。 Note that the connection will be blocking, so it can tie up your server depending on how quickly the remote host returns the HTTP response and how many of these requests you are making. 请注意,连接将被阻塞,因此它可以绑定服务器,具体取决于远程主机返回HTTP响应的速度以及发出的请求数量。

require 'net/http'

# Let Ruby form a canonical URI from our URL
ping_uri = URI('https://api.cloud-elements.com/elements/api-v2/hubs/marketing/ping')

# Pass the basic configuration to Net::HTTP
# Note, this is not asynchronous. Ruby will wait until the HTTP connection
# has closed before moving forward
Net::HTTP.start(ping_uri.host, ping_uri.port, :use_ssl => true) do |http|
  # Build the request using the URI as a Net::HTTP::Get object
  request = Net::HTTP::Get.new(ping_uri)
  # Add the Authorization header
  request['Authorization'] = "Element #{ELEMENT_TOKEN}, User #{user.token}"

  # Actually send the request
  response = http.request(request)
  # Ruby will automatically close the connection once we exit the block
end

Once the block exits, you can use the response object as necessary. 块退出后,您可以根据需要使用response对象。 The response object is always a subclass (or subclass of a subclass) of Net::HTTPResponse and you can use response.is_a? Net::HTTPSuccess response对象始终是Net::HTTPResponse的子类(或子类的子类),您可以使用response.is_a? Net::HTTPSuccess response.is_a? Net::HTTPSuccess to check for a 2xx response. response.is_a? Net::HTTPSuccess检查2xx响应。 The actual body of the response will be in response.body as a String. 响应的实际主体将在response.body作为字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM