我有一个非常简单的ruby脚本,我从一个crontab条目调用它,整个脚本如下:
require 'rest-client'
require 'json'
valve_id, gpio_pin, cmd, http_host = ARGV
# puts "valve_id --> #{valve_id}, cmd --> #{cmd}, http_host --> #{http_host}"
RestClient.put "http://#{http_host}/api/valves/#{valve_id}.json", { "params" => {"cmd" => "#{cmd}"}}.to_json , :content_type => :json, :accept => :json下面是crontab条目:
ruby /home/kenb/development/CoffeeWater/lib/tasks/rest_client.rb 5 23 0 stargate:9292被访问的资源是一个简单的rails模型Valve,具有一个整数属性'cmd‘。
我似乎就是不能正确地编码,我一直收到错误400。
发布于 2015-06-30 01:33:20
我已经试过你的代码了,一切用我自己的url http://zoker.sinaapp.com/sonar.php都运行得很好。
我认为你的api接口有问题,也许你应该用begin...rescue缓存错误消息,看看你的api出了什么问题。
以下是我对您的代码进行的测试
ruby.rb
require 'rest-client'
require 'json'
valve_id, gpio_pin, cmd, http_host = ARGV
# puts "valve_id --> #{valve_id}, cmd --> #{cmd}, http_host --> #{http_host}"
begin
RestClient.put "http://zoker.sinaapp.com/sonar.php", { "params" => {"cmd" => "#{cmd}","valve_id"=>"#{valve_id}","http_host"=>"#{http_host}"}}.to_json , :content_type => :json, :accept => :json
rescue => e
puts e
end 使用ruby ruby.rb 5 23 0 stargate:9292运行它
下面是我的api得到的结果
{"params":{"cmd":"0","valve_id":"5","http_host":"stargate:9292"}}---------X-Forwarded-For: 112.97.38.29, 112.97.38.29------------------Host: zoker.sinaapp.com------------------AppName: zoker------------------AppVersion: 1------------------AccessKey: loy5m01zk1------------------AppHash: 132------------------MysqlPort: 3321------------------AppCookie: default_version=1;debug=1;------------------AppSrvc: 0000000000000000------------------Connection: close------------------Content-Length: 65------------------Accept: application/json------------------Accept-Encoding: gzip, deflate------------------Content-Type: application/json------------------User-Agent: Ruby---------您可以看到结果here
发布于 2015-06-30 03:33:36
下面是development.log:
已开始在2015-06-29 12:06:18 -0700 [1m[36mActiveRecord::SchemaMigration Load (0.1ms)][0M [1mSELECT "schema_migrations".* FROM "schema_migrations"[0m Processing by ValvesController#update as JSON Parameters:{"params"=>{"id"=>"1","cmd"=>"0"},"id"=>"1","valve"=>{}} [1m[35mValve Load (0.2ms)][0M SELECT "valves".* "valves“WHERE "valves"."id”=?LIMIT 1 ["id",1] [1m[36mWaterManager Load (0.1ms)][0M [1mSELECT "water_managers".* FROM "water_managers“ORDER BY”water_managers“。”id“ASC LIMIT 1[0m在35ms内完成了400个错误请求(ActiveRecord: 1.1ms) )
ActionController::ParameterMissing -参数缺失或值为空: value : actionpack (4.2.1) lib/action_controller/metal/strong_parameters.rb:249:in require' puma (2.11.3) lib/puma/server.rb:262:inblock in run‘puma (2.11.3) lib/puma/thread_pool.rb:104:in’spawn_thread‘
神秘的是参数‘阀门’,也就是模型的名字。下面是生成此响应的修改后的ruby脚本:
#
# a REST client, used to put valve updates into the server. This script is intended to be invoked from a crontab operation
# invoked as:
# ruby /home/kenb/development/CoffeeWater/tasks/rest_client.rb valve_id gpio_pin cmd http_host
require 'rest-client'
require 'json'
valve_id, gpio_pin, cmd, http_host = ARGV
begin
RestClient.put "http://#{http_host}/valves/#{valve_id}.json", { "params" => { "id" => "#{valve_id}", "cmd" => "#{cmd}"}}.to_json , :content_type => :json, :accept => :json
rescue => e
puts e
end发布于 2015-07-01 04:30:24
我的问题解决了。我添加了一个外部散列:params => {...},这导致rest-client抛出一个额外的参数'valve {}',这是rails服务器不喜欢的。
我也不知道原因。被访问的rails模型是'valves',在rest-client请求中没有对valve的引用。
无论如何,删除外部参数散列解决了我的问题。
https://stackoverflow.com/questions/31121214
复制相似问题