Ruby faraday openweathermap api configure block and then conn.get block
2017-11-15 17:48:59
收藏:0
阅读:173
评论:1
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 响应,因此很明显获取请求没有正确地工作。检查后,我发现最终的 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 和请求 / 响应循环。
有人可以给我一些建议或在这个主题上提供一些帮助吗?
点赞
评论区的留言会收到邮件通知哦~
推荐文章
- Lua 虚拟机加密load(string.dump(function)) 后执行失败问题如何解决
- 我想创建一个 Nginx 规则,禁止访问
- 如何将两个不同的lua文件合成一个 东西有点长 大佬请耐心看完 我是小白研究几天了都没搞定
- 如何在roblox studio中1:1导入真实世界的地形?
- 求解,lua_resume的第二次调用继续执行协程问题。
- 【上海普陀区】内向猫网络招募【Skynet游戏框架Lua后端程序员】
- SF爱好求教:如何用lua实现游戏内调用数据库函数实现账号密码注册?
- Lua实现网站后台开发
- LUA错误显式返回,社区常见的规约是怎么样的
- lua5.3下载库失败
- 请问如何实现文本框内容和某个网页搜索框内容连接,并把网页输出来的结果反馈到另外一个文本框上
- lua lanes多线程使用
- 一个kv数据库
- openresty 有没有比较轻量的 docker 镜像
- 想问一下,有大佬用过luacurl吗
- 在Lua执行过程中使用Load函数出现问题
- 为什么 neovim 里没有显示一些特殊字符?
- Lua比较两个表的值(不考虑键的顺序)
- 有个lua简单的项目,外包,有意者加微信 liuheng600456详谈,最好在成都
- 如何在 Visual Studio 2022 中运行 Lua 代码?

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"