我有一个非常简单的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.
答案 0 :(得分:0)
我已尝试过您的代码,一切正常,我自己的网址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
答案 1 :(得分:0)
这是development.log:
开始PUT" /valves/1.json"对于2015.0-29 12:06:18 -0700的127.0.0.1 [1m [36mActiveRecord :: SchemaMigration Load(0.1ms)[0m [1mSELECT" schema_migrations"。* FROM" schema_migrations" [0m] 由ValvesController处理#update为JSON 参数:{" params" => {" id" =>" 1"," cmd" =>" 0"}," id" =>" 1"," valve" => {}} [1m [35mValve Load(0.2ms)[0m SELECT" valve"。* FROM" valve"阀门"。" id" =?限制1 [[" id",1]] [1m [36mWaterManager Load(0.1ms)[0m [1mSELECT" water_managers"。* FROM" water_managers"订购" water_managers"。" id" ASC LIMIT 1 [0m 在35毫秒内完成了400个错误请求(ActiveRecord:1.1毫秒)
ActionController :: ParameterMissing - 缺少param或值为空:valve:
actionpack(4.2.1)lib / action_controller / metal / strong_parameters.rb:249:在require'
puma (2.11.3) lib/puma/server.rb:262:in
块中运行'
puma(2.11.3)lib / puma / thread_pool.rb:104:在spawn_thread中的`block'
神秘的是param& t"这是模型的名称。以下是产生此响应的修改后的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
答案 2 :(得分:0)
我的问题已经解决了。我添加了一个外部哈希,:params => {...},这导致rest-client抛出一个额外的参数'valve - {}',这是rails服务器不喜欢的。
我不知道为什么。正在访问的轨道模型是“阀门”,并且在休息客户机请求中没有对阀门的引用。
无论如何,删除外部参数哈希解决了我的问题。