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
The above is the current configure block and request block I have been using, unfortunately it is not producing the results I want.
I receive:
#<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=[]>
I first see a 404 response so its obvious the get request did not work correctly. Upon inspection I see that the final url is not encoding the params correctly. The url as of now is:
#<URI::HTTP http://api.openweathermap.org/weather?APPID=95cade087f6f767d179feaa301816de4&q=oskaloosa&units=metric>
when in reality the correct url I am trying to construct is:
#<URI::HTTP http://api.openweathermap.org/weather?q=oskaloosa&APPID=95cade087f6f767d179feaa301816de4&units=metric>
.
Now I know I can string interpolate with #{param['key'])
or #{@key}` , but I am trying to use solely block construction for this conn and request/response cycle.
Can anyone give me some advice or shed some light on this topic?
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
response:
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"
via @pdoherty926 in the comments above