无法从Ruby使用opt_expand工作(使用在curl中工作的相同代码)

问题描述:

我已经编写了一个简短的Ruby脚本,它创建了一个很好的将ASANA任务导出到csv的过程,但是它需要一段时间才能运行,因为我有为每个项目中的每项任务执行GET操作。 我发现使用opt_expand获得一次所有任务数据对每一个项目,这使得随后数的“获取” S它是目前的一小部分的方式。 然而这在卷曲工作的opt_expand代码不能在Ruby中工作,它只是忽略了expand命令。无法从Ruby使用opt_expand工作(使用在curl中工作的相同代码)

任何帮助将不胜感激,

普通卷曲代码[snippet1]:

curl -u <token>: https://app.asana.com/api/1.0/projects/<project_id>/tasks 

工作opt_expand卷曲代码[snippet2]:

curl -u <token>: https://app.asana.com/api/1.0/projects/<project_id>/tasks?opt_expand=. 

普通Ruby代码[snippet3] :

uri = URI.parse("https://app.asana.com/api/1.0/projects/<project_id>") 

http = Net::HTTP.new(uri.host, uri.port) 
http.use_ssl = true 
... 

破碎Ruby代码尽管使用opt_expand

uri = URI.parse"(https://app.asana.com/api/1.0/projects/<project_id>/tasks?opt_expand=. 
") 

http = Net::HTTP.new(uri.host, uri.port) 
http.use_ssl = true 
... 
+0

我猜你是以这样的方式发出请求,以便不发送URL参数。关键是你的''...在你的代码段正在做什么。你能在那里提供细节吗? –

这是一个有点困难没有看到错误信息(或服务器返回的消息),你得到答案,返回相同的片段3。

但是请记住,Net :: HTTP是低级别的,对于这样一个简单的任务可能有点矫枉过正。你有没有考虑使用其他库(即:rest-client或法拉第),这将是您更容易与工作。例如:

require 'rest_client' 

response = RestClient.get "https://app.asana.com/api/1.0/projects/<project_id>" 
if response.code == 200 
    # process answer 
end 
+0

嗨,谢谢你,你完全正确我的任务过于复杂,并通过它查看是我根本没有发送查询,只是路径。这里是我改变了: req = Net :: HTTP :: Get.new(uri.path,header) req = Net :: HTTP :: Get.new(urip,header) – user1468963

+0

其中urip = uri.path + '?' + uri.query谢谢! – user1468963

像Greg说的那样,你没有将params传递给服务器。 Ruby URI解析不包含参数。尝试这样的事情:

params = { :opt_expand => 'your opt here' } 
uri = URI.parse("https://app.asana.com/api/1.0/projects/<project_id>/tasks") 
http = Net::HTTP.new(uri.host, uri.port) 
... 
uri_with_params = "#{uri.path}?".concat(params.collect { |k,v| "#{k}=#{CGI::escape(v.to_s)}" }.join('&')) if not params.nil? 
req = Net::HTTP::Get.new(uri_with_params, header) 
req.basic_auth(key, password) 
res = http.start { |http| http.request(req) }