1

I am trying to view the JSON request which is sent from POSTMAN through a POST request to add a security group info to a table, and my request looks like one below

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 code looks like below

# 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

Can you please take a look at the request and point me, where am I going wrong, Request format not correct or is there a different way to handle the request on the ruby code.

I need help to parse the request that comes in as JSON similar to code presented in 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
the Tin Man
  • 158,662
  • 42
  • 215
  • 303

1 Answers1

1

You need just a little tweak: Add to the App the plugin :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

See in the Roda documentation "Plugins that Ship with Roda" in the group "others" there is the json_parser plugin.

Alejandro Babio
  • 5,189
  • 17
  • 28
  • Thanks for your response Babio, but I couldn't find the right gem to be installed to handle the request [json_parse]. I tried to install gem install json_parse but I got the following response C:\ruby\prodcat\lib\api>gem install json_parse ERROR: Could not find a valid gem 'json_parse' (>= 0) in any repository ERROR: Possible alternatives: json-parser, json_parser, json_pure, gson_parser, json_tree could you please help me to identify the correct gem that needs to be installed – Balaji Mohandas Dec 05 '17 at 14:06
  • It is part of the 'roda' gem. You only need to add one line to your code. See it here https://github.com/jeremyevans/roda/blob/master/lib/roda/plugins/json_parser.rb – Alejandro Babio Dec 05 '17 at 18:08
  • I guess I confused you because I wrote wrong the name of the plugin, it is `json_parser` not `json_parse`. – Alejandro Babio Dec 05 '17 at 21:54
  • Thanks a bunch Babio, that worked class App < Roda plugin :json, classes: [Array, Hash, Sequel::Model, Sequel::Dataset] plugin :json_parser – Balaji Mohandas Dec 05 '17 at 23:18