【问题标题】:ORM JSON request handling for dataset insert数据集插入的 ORM JSON 请求处理
【发布时间】:2018-05-18 03:12:18
【问题描述】:

我正在尝试查看 POSTMAN 通过 POST 请求发送的 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​​ 代码上的请求。

我需要帮助来解析以 JSON 形式传入的请求,类似于 https://twin.github.io/introduction-to-roda/ 中提供的代码

  r.post "albums" do
    album = Album.create(r.params["album"])
    r.redirect album_path(album) # /albums/1
  end

【问题讨论】:

    标签: ruby sequel roda


    【解决方案1】:

    您只需稍作调整:将插件: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 文档 "Plugins that Ship with Roda" 在“其他”组中的 json_parser 插件。

    【讨论】:

    • 感谢您的回复 Babio,但我找不到要安装的正确 gem 来处理请求 [json_parse]。我尝试安装 gem install json_parse 但得到以下响应 C:\ruby\prodcat\lib\api>gem install json_parse 错误:在任何存储库中找不到有效的 gem 'json_parse' (>= 0) 错误:可能的替代方案: json-parser, json_parser, json_pure, gson_parser, json_tree 你能帮我确定需要安装的正确 gem吗
    • 它是“roda”宝石的一部分。您只需在代码中添加一行。在这里查看github.com/jeremyevans/roda/blob/master/lib/roda/plugins/…
    • 我想我把你弄错了,因为我写错了插件的名字,它是json_parser而不是json_parse。
    • 感谢一堆 Babio,工作类 App
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-18
    • 2020-10-25
    • 2021-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多