Ruby faraday openweathermap api 配置块然后 conn.get 块
Ruby faraday openweathermap api configure block and then conn.get block
require 'faraday'
require 'pry'
ENV['API'] = "XXXXXXXXXXXX"
conn = Faraday.new(:url => 'http://api.openweathermap.org/data/2.5') do|faraday|
faraday.request :url_encoded # form-encode POST params
faraday.response :logger # log requests to STDOUT
faraday.adapter Faraday.default_adapter # make requests with
Net::HTTP
end
response = conn.get do |req|
req.url '/weather'
req.params['q'] = 'oskaloosa'
req.params['APPID'] = ENV['API']
req.params['units'] = 'metric'
end
以上是我目前使用的配置块和请求块,不幸的是它没有产生我想要的结果。
我收到:
#<Faraday::Response:0x00007f7f4aa331f0
@env=
#<struct Faraday::Env
method=:get,
body=
"<html>\r\n<head><title>404 Not Found</title></head>\r\n<body
bgcolor=\"white\">\r\n<center><h1>404 Not Found</h1></center>\r\n<hr>
<center>nginx</center>\r\n</body>\r\n</html>\r\n",
url=
#<URI::HTTP http://api.openweathermap.org/weather?
APPID=xxxxxxxxxxxxxxxxx&q=oskaloosa&units=metric>,
request=
#<struct Faraday::RequestOptions
params_encoder=nil,
proxy=nil,
bind=nil,
timeout=nil,
open_timeout=nil,
boundary=nil,
oauth=nil,
context=nil>,
request_headers={"User-Agent"=>"Faraday v0.13.1"},
ssl=
#<struct Faraday::SSLOptions
verify=nil,
ca_file=nil,
ca_path=nil,
verify_mode=nil,
cert_store=nil,
client_cert=nil,
client_key=nil,
certificate=nil,
private_key=nil,
verify_depth=nil,
version=nil>,
parallel_manager=nil,
params=nil,
response=#<Faraday::Response:0x00007f7f4aa331f0 ...>,
response_headers=
{"server"=>"openresty",
"date"=>"Sun, 12 Nov 2017 22:55:30 GMT",
"content-type"=>"text/html",
"content-length"=>"162",
"connection"=>"close"},
status=404,
reason_phrase="Not Found">,
@on_complete_callbacks=[]>
我第一次看到 404 响应,很明显 get 请求没有正常工作。经过检查,我发现最后的 url 没有正确编码参数。 url 目前是:
#<URI::HTTP http://api.openweathermap.org/weather?APPID=95cade087f6f767d179feaa301816de4&q=oskaloosa&units=metric>
而实际上我试图构建的正确 url 是:
#<URI::HTTP http://api.openweathermap.org/weather?q=oskaloosa&APPID=95cade087f6f767d179feaa301816de4&units=metric>
.
现在我知道我可以用 #{param['key'])
或 #{@key}` 进行字符串插值,但我正尝试只为这个 conn 和 request/response 循环使用块构造。
任何人都可以给我一些建议或阐明这个话题吗?
require 'faraday'
require 'pry'
ENV['API'] = "95cade087f6f767d179feaa301816de4"
conn = Faraday.new(:url => 'http://api.openweathermap.org') do |faraday|
faraday.request :url_encoded # form-encode POST params
faraday.response :logger # log requests to STDOUT
faraday.adapter Faraday.default_adapter # make requests with Net::HTTP
end
response = conn.get do |req|
req.url '/data/2.5/weather'
req.params['q'] = 'oskaloosa'
req.params['APPID'] = ENV['API']
req.params['units'] = 'metric'
end
回复:
I, [2017-11-12T17:57:43.154891 #15447] INFO -- : get http://api.openweathermap.org/data/2.5/weather?APPID=95cade087f6f767d179feaa301816de4&q=oskaloosa&units=metric
D, [2017-11-12T17:57:43.154968 #15447] DEBUG -- request: User-Agent: "Faraday v0.13.1"
I, [2017-11-12T17:57:43.717003 #15447] INFO -- Status: 200
D, [2017-11-12T17:57:43.717153 #15447] DEBUG -- response: server: "openresty"
date: "Sun, 12 Nov 2017 23:57:50 GMT"
content-type: "application/json; charset=utf-8"
content-length: "428"
connection: "close"
x-cache-key: "/data/2.5/weather?APPID=95cade087f6f767d179feaa301816de4&q=oskaloosa&units=metric"
access-control-allow-origin: "*"
access-control-allow-credentials: "true"
access-control-allow-methods: "GET, POST"
通过上面评论中的@pdoherty926
require 'faraday'
require 'pry'
ENV['API'] = "XXXXXXXXXXXX"
conn = Faraday.new(:url => 'http://api.openweathermap.org/data/2.5') do|faraday|
faraday.request :url_encoded # form-encode POST params
faraday.response :logger # log requests to STDOUT
faraday.adapter Faraday.default_adapter # make requests with
Net::HTTP
end
response = conn.get do |req|
req.url '/weather'
req.params['q'] = 'oskaloosa'
req.params['APPID'] = ENV['API']
req.params['units'] = 'metric'
end
以上是我目前使用的配置块和请求块,不幸的是它没有产生我想要的结果。
我收到:
#<Faraday::Response:0x00007f7f4aa331f0
@env=
#<struct Faraday::Env
method=:get,
body=
"<html>\r\n<head><title>404 Not Found</title></head>\r\n<body
bgcolor=\"white\">\r\n<center><h1>404 Not Found</h1></center>\r\n<hr>
<center>nginx</center>\r\n</body>\r\n</html>\r\n",
url=
#<URI::HTTP http://api.openweathermap.org/weather?
APPID=xxxxxxxxxxxxxxxxx&q=oskaloosa&units=metric>,
request=
#<struct Faraday::RequestOptions
params_encoder=nil,
proxy=nil,
bind=nil,
timeout=nil,
open_timeout=nil,
boundary=nil,
oauth=nil,
context=nil>,
request_headers={"User-Agent"=>"Faraday v0.13.1"},
ssl=
#<struct Faraday::SSLOptions
verify=nil,
ca_file=nil,
ca_path=nil,
verify_mode=nil,
cert_store=nil,
client_cert=nil,
client_key=nil,
certificate=nil,
private_key=nil,
verify_depth=nil,
version=nil>,
parallel_manager=nil,
params=nil,
response=#<Faraday::Response:0x00007f7f4aa331f0 ...>,
response_headers=
{"server"=>"openresty",
"date"=>"Sun, 12 Nov 2017 22:55:30 GMT",
"content-type"=>"text/html",
"content-length"=>"162",
"connection"=>"close"},
status=404,
reason_phrase="Not Found">,
@on_complete_callbacks=[]>
我第一次看到 404 响应,很明显 get 请求没有正常工作。经过检查,我发现最后的 url 没有正确编码参数。 url 目前是:
#<URI::HTTP http://api.openweathermap.org/weather?APPID=95cade087f6f767d179feaa301816de4&q=oskaloosa&units=metric>
而实际上我试图构建的正确 url 是:
#<URI::HTTP http://api.openweathermap.org/weather?q=oskaloosa&APPID=95cade087f6f767d179feaa301816de4&units=metric>
.
现在我知道我可以用 #{param['key'])
或 #{@key}` 进行字符串插值,但我正尝试只为这个 conn 和 request/response 循环使用块构造。
任何人都可以给我一些建议或阐明这个话题吗?
require 'faraday'
require 'pry'
ENV['API'] = "95cade087f6f767d179feaa301816de4"
conn = Faraday.new(:url => 'http://api.openweathermap.org') do |faraday|
faraday.request :url_encoded # form-encode POST params
faraday.response :logger # log requests to STDOUT
faraday.adapter Faraday.default_adapter # make requests with Net::HTTP
end
response = conn.get do |req|
req.url '/data/2.5/weather'
req.params['q'] = 'oskaloosa'
req.params['APPID'] = ENV['API']
req.params['units'] = 'metric'
end
回复:
I, [2017-11-12T17:57:43.154891 #15447] INFO -- : get http://api.openweathermap.org/data/2.5/weather?APPID=95cade087f6f767d179feaa301816de4&q=oskaloosa&units=metric
D, [2017-11-12T17:57:43.154968 #15447] DEBUG -- request: User-Agent: "Faraday v0.13.1"
I, [2017-11-12T17:57:43.717003 #15447] INFO -- Status: 200
D, [2017-11-12T17:57:43.717153 #15447] DEBUG -- response: server: "openresty"
date: "Sun, 12 Nov 2017 23:57:50 GMT"
content-type: "application/json; charset=utf-8"
content-length: "428"
connection: "close"
x-cache-key: "/data/2.5/weather?APPID=95cade087f6f767d179feaa301816de4&q=oskaloosa&units=metric"
access-control-allow-origin: "*"
access-control-allow-credentials: "true"
access-control-allow-methods: "GET, POST"
通过上面评论中的@pdoherty926