Ruby on rails 4 Grape POST请求接收JSON
我对葡萄有问题。 我试着查看他们的文档和谷歌搜索。 我找不到任何解决方案或样品 假设我将这种格式发送给grape的POST请求:Ruby on rails 4 Grape POST请求接收JSON,ruby-on-rails-4,grape,grape-api,Ruby On Rails 4,Grape,Grape Api,我对葡萄有问题。 我试着查看他们的文档和谷歌搜索。 我找不到任何解决方案或样品 假设我将这种格式发送给grape的POST请求: { "preferences": { "play": { "weekdays": "5", "weekend": "8" }, "grow": { "weekdays": "4", "weekend": "8"
{
"preferences": {
"play": {
"weekdays": "5",
"weekend": "8"
},
"grow": {
"weekdays": "4",
"weekend": "8"
}
}
}
问题:
非常感谢您回答问题1:如果所有参数都是可选的,那么您甚至不需要
参数
块。摆脱它,例如,使用params[:preferences][:play][:weekdays]
访问weekdays属性。只需使用相同的想法访问其他值
回答问题2:在Postman上,使用RAW,但不要忘记将标题
内容类型设置为application/json
。我编写了下面的代码来处理这个示例
require 'grape'
class API < Grape::API
version 'v1', :using => :header, :vendor => 'alienlabz', :format => :json
format :json
resource :preferences do
post do
puts params[:preferences][:play][:weekdays]
end
end
end
run API
需要“葡萄”
类API:header,:vendor=>'alienlabz',:format=>:json
格式:json
资源:首选项是什么
邮政局
放置参数[:首选项][:播放][:工作日]
结束
结束
结束
运行API
您找到解决方案了吗?
require 'grape'
class API < Grape::API
version 'v1', :using => :header, :vendor => 'alienlabz', :format => :json
format :json
resource :preferences do
post do
puts params[:preferences][:play][:weekdays]
end
end
end
run API