Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 散列/字符串被转义_Ruby_String_Hash_Escaping_Sinatra - Fatal编程技术网

Ruby 散列/字符串被转义

Ruby 散列/字符串被转义,ruby,string,hash,escaping,sinatra,Ruby,String,Hash,Escaping,Sinatra,这是我的超级资源客户端: require 'rubygems' require 'hyperresource' require 'json' api = HyperResource.new(root: 'http://127.0.0.1:9393/todos', headers: {'Accept' => 'application/vnd.127.0.0.1:9393/todos.v1+hal+json'

这是我的超级资源客户端:

require 'rubygems'
require 'hyperresource'
require 'json'

api = HyperResource.new(root: 'http://127.0.0.1:9393/todos',                      
                    headers: {'Accept' => 'application/vnd.127.0.0.1:9393/todos.v1+hal+json'})
string = '{"todo":{"title":"test"}}'
hash = JSON.parse(string) 
api.post(hash)
puts hash
散列输出为:{todo=>{title=>test}

在我的Sinatra with Roar API中,我有以下post功能:

post "/todos" do
  params.to_json
  puts params
  @todo = Todo.new(params[:todo])
  if @todo.save  
    @todo.extend(TodoRepresenter)
    @todo.to_json 
  else 
    puts 'FAIL'
  end 
end 
我把'params'放在这里得到:{{\todo\:{\title\:\test\}=>nil}

我发现,这些是“转义字符串”,但我不知道哪里出错了

编辑:


我用curl和postman-google扩展检查了我的api,两者都工作得很好。我猜这只是一个超级资源,你发布的是JSON,因此你要么需要注册一个Sinatra中间件来自动解析传入的JSON请求,要么需要自己完成

require 'rubygems'
require 'hyperresource'
require 'json'

api = HyperResource.new(root: 'http://127.0.0.1:9393/todos',                      
                    headers: {'Accept' => 'application/vnd.127.0.0.1:9393/todos.v1+hal+json'})
string = '{"todo":{"title":"test"}}'
hash = JSON.parse(string) 
api.post({:data => hash})
puts hash    

---

post "/todos" do
  p = JSON.parse(params[:data])
  puts p.inspect
  @todo = Todo.new(p[:todo])
  if @todo.save  
    @todo.extend(TodoRepresenter)
    @todo.to_json 
  else 
    puts 'FAIL'
  end 
end 

应该做你需要的。

我的客户有500个错误。服务器控制台中的原因是:TypeError-没有将nil隐式转换为字符串:。所以,我猜:数据试图解析nil部分,而不是整个数据hash@mcfinnigan