我正在尝试查看通过邮政请求发送的JSON请求,以将安全组信息添加到表中,而我的请求看起来像下面的一个
POST /securitygroup HTTP/1.1
Host: localhost:9292
Content-Type: application/json
Cache-Control: no-cache
Postman-Token: c4bef1db-d544-c923-3b0b-e7004e2dd093
{
"securitygroup":{
"secgrp_id": 124,
"secgrp_nm": "SECURITY ADMIN",
"secgrp_profile_nme": "ADMIN"
}
}
Roda代码如下
# cat config.ru
require "roda"
require "sequel"
require "oci8"
require "json"
DB = Sequel.oracle(host: 'xyz.dev.com', port: '1525', database: 'devbox1', user: 'abc', password: 'pass')
class App < Roda
plugin :json, classes: [Array, Hash, Sequel::Model, Sequel::Dataset]
route do |r|
# secgroup = DB[:security_groups]
# secgroup.insert(r.params["securitygroup"])
# secgroup
# above insert threw the following error
# OCIError: ORA-00947: not enough values,
# because SQL generated as below
# INSERT INTO "SECURITYGROUPS" VALUES (NULL)
# so I am trying to access the request object 'r', I feel that I am doing
# something which is not correct
{"response": r.params.["securitygroup"]["secgrp_id"]}
# throws undefined method `[]' for nil:NilClass
end
end
您可以查看请求并指向我,我要在哪里出错,请求格式不正确或有其他方法可以处理Ruby Code上的请求。
我需要帮助来解析与https://twin.github.io/introduction-to-to-roda/
中提出的代码相似的JSON的请求。 r.post "albums" do
album = Album.create(r.params["album"])
r.redirect album_path(album) # /albums/1
end
您只需要一点调整:添加到应用程序插件:json_parser
。
class App < Roda
# use this plugin to convert response to json
plugin :json
# use this plugin to convert request from json
plugin :json_parser
...
end
请参阅RODA文档中的"与Roda一起运送的插件","其他"中有json_parser
插件。