rails - 如何向URL发出GET请求

时间:2022-08-22 17:42:16

Hello there I'm hoping this problem is a fairly simple one as I am relatively new to rails development. I am trying to make a get request to a URL.For Example on success on certain action I need to make a get request to a URL with my parameters

您好,我希望这个问题相当简单,因为我对rails开发相对较新。我正在尝试向URL发出get请求。有关某些操作成功的示例,我需要使用我的参数向URL发出get请求

http://abc.com/xyz.php?to=<%user.number%>&sender=TESTHC&message="MY MESSAGE"!

3 个解决方案

#1


1  

read this http://ruby-doc.org/stdlib-1.9.3/libdoc/net/http/rdoc/Net/HTTP.html you will get good examples to refer.

阅读此http://ruby-doc.org/stdlib-1.9.3/libdoc/net/http/rdoc/Net/HTTP.html您将获得很好的参考示例。

#2


1  

This might help. There is a gem called Faraday. It is used to make http request. Here is an example usage:

这可能有所帮助。有一种名为法拉第的宝石。它用于发出http请求。以下是一个示例用法:

Build a connection

建立连接

conn = Faraday.new(:url => 'http://sushi.com') do |builder|
  builder.use Faraday::Request::UrlEncoded  # convert request params as "www-form-urlencoded"
  builder.use Faraday::Response::Logger     # log the request to STDOUT
  builder.use Faraday::Adapter::NetHttp     # make http requests with Net::HTTP

  # or, use shortcuts:
  builder.request  :url_encoded
  builder.response :logger
  builder.adapter  :net_http
end

Make your Get request

提出您的Get请求

conn.get '/nigiri', { :name => 'Maguro' } # GET /nigiri?name=Maguro

#3


1  

Your question, as posed, has nothing to do with Rails or routes. If you want to make an HTTP request, whether from a Rails controller or a standalone Ruby program, you can use a standard library such as open-uri.

你提出的问题与Rails或路由无关。如果您想要发出HTTP请求,无论是来自Rails控制器还是独立的Ruby程序,您都可以使用标准库,例如open-uri。

As an example:

举个例子:

require 'open-uri'
to, sender, message = 12345, 'foo', 'bar'
uri='http://abc.com/xyz.php?to=%d&sender=%s&message="%s"' % [to, sender, message]
open(uri).readlines

#1


1  

read this http://ruby-doc.org/stdlib-1.9.3/libdoc/net/http/rdoc/Net/HTTP.html you will get good examples to refer.

阅读此http://ruby-doc.org/stdlib-1.9.3/libdoc/net/http/rdoc/Net/HTTP.html您将获得很好的参考示例。

#2


1  

This might help. There is a gem called Faraday. It is used to make http request. Here is an example usage:

这可能有所帮助。有一种名为法拉第的宝石。它用于发出http请求。以下是一个示例用法:

Build a connection

建立连接

conn = Faraday.new(:url => 'http://sushi.com') do |builder|
  builder.use Faraday::Request::UrlEncoded  # convert request params as "www-form-urlencoded"
  builder.use Faraday::Response::Logger     # log the request to STDOUT
  builder.use Faraday::Adapter::NetHttp     # make http requests with Net::HTTP

  # or, use shortcuts:
  builder.request  :url_encoded
  builder.response :logger
  builder.adapter  :net_http
end

Make your Get request

提出您的Get请求

conn.get '/nigiri', { :name => 'Maguro' } # GET /nigiri?name=Maguro

#3


1  

Your question, as posed, has nothing to do with Rails or routes. If you want to make an HTTP request, whether from a Rails controller or a standalone Ruby program, you can use a standard library such as open-uri.

你提出的问题与Rails或路由无关。如果您想要发出HTTP请求,无论是来自Rails控制器还是独立的Ruby程序,您都可以使用标准库,例如open-uri。

As an example:

举个例子:

require 'open-uri'
to, sender, message = 12345, 'foo', 'bar'
uri='http://abc.com/xyz.php?to=%d&sender=%s&message="%s"' % [to, sender, message]
open(uri).readlines