Testing with Postman keeps returning Invalid JSON error - ruby

I have created REST Api in Ruby on Sinatra platform. I am testing the service with Postman and whatever JSON form I try to POST I keep getting an error 400 Invalid JSON SUCKER. The error is defined on the back end in case of invalid JSON form. Please take a look at the back end and tell me what am I doing wrong.
I have to mention that GET method works with Postman and cURL from the command line while POST works only if I use it with cURL but NOT in POSTMAN.
#server.rb
require 'sinatra'
require 'mongoid'
require 'sinatra/namespace'
require 'sinatra/base'
#require 'json'
before do
content_type :json
headers 'Access-Control-Allow-Origin' => '*',
'Access-Control-Allow-Methods' => ['OPTIONS', 'GET', 'POST', 'PATCH']
end
#load database config
Mongoid.load! "mongoid.config"
#Class Company
class Company
include Mongoid::Document
field :compid, type: String
field :name, type: String
field :address, type: String
field :city, type: String
# validates :compid, presence: true
# validates :name, presence: true
index({ compid:1 }, { unique: true, name: "compid_index" })
index({ name: 'text' })
scope :name, -> (name) { where(name: /^#{name}/) } # this could be changed to /^#{title}/i to make case insensitive searcg =end
scope :compid, -> (compid) { where(compid: compid) }
end
#Serializers
class Serializer
def initialize(company)
#company = company
end
def as_json(*)
data ={
id:#company.compid.to_s,
name:#company.name,
address:#company.address,
city:#company.city,
}
data[:errors] = #company.errors if#company.errors.any?
data
end
end
# Endpoints
get '/' do
'List of all Companies'
end
namespace '/api/v1' do
before do
content_type 'application/json'
end
helpers do
def base_url
#base_url ||= "#{request.env['rack.url_scheme']}://{request.env['HTTP_HOST']}"
end
def json_params
begin
JSON.parse(request.body.read)
rescue
halt 400, { message:'Invalid JSON' }.to_json
end
end
end
get '/companies' do
companies = Company.all
[ :name, :compid,].each do |filter|
companies = companies.send(filter, params[filter]) if params[filter]
end
#put it through the serializer not to get all te atributes
companies.map { |company| Serializer.new(company) }.to_json
end
get '/companies/:compid' do |compid| #get the details about the company by searching with compid
company = Company.where(compid: compid).first
halt(404, { message:'Company Not Found'}.to_json) unless company
Serializer.new(company).to_json
end
post '/companies' do
company = Company.new(json_params)
if company.save
response.headers['Location'] = "#{base_url}/api/v1/companies{company.copmid}" # "{company.id}"
status 201
else
status 422
body Serializer.new(company).to_json
end
end
The data that I GET with the Postman looks like this:
[{"id":"5a1271f7943e8a0f5fd76008","name":"The Power Of Habit","address":"Charles Duhigg Vej","city":"Viborg"}]
I have tried to POST data in various forms:
[{"id":"5a1271f79asdd76008","name":"The Power Of Habit","address":"Charles Duhigg Vej","city":"Viborg"}]
{"id":"5a1271f79asdd76008","name":"The Power Of Habit","address":"Charles Duhigg Vej","city":"Viborg"}
[{"compid":"5a1271f79asdd76008","name":"The Power Of Habit","address":"Charles Duhigg Vej","city":"Viborg"}]
{"compid":"5a1271f79asdd76008","name":"The Power Of Habit","address":"Charles Duhigg Vej","city":"Viborg"}

Related

PG::UndefinedTable: ERROR: relation "cities" does not exist LINE 8: WHERE a.attrelid = '"cities"'::regclass ^

I am new to Sinatra and trying to create a post request which would add some data into "City" table in PGadmin. But, I am getting an error when trying to add it. The get request works fine, and posting static data on the post endpoint also works fine, but posting data doesn't I am not sure what's wrong since the migration and the model for city does exist. Here's the code:
city.rb
class City < UserDB
validates_presence_of :name, :pin
def as_json(options = {})
super({only: [:city, :pin]})
end
end
migration:
class CreateCity < ActiveRecord::Migration[6.0]
def change
create_table :city do |t|
t.string :name
t.integer :pin
end
end
end
module Users
module V1
class CityController< LocalAPI::ApplicationBase
before do
content_type 'application/json'
end
get '/' do
city=City.all
if city
"city exists"
else
"doesn't exist"
end
end
post '/add' do
params = JSON.parse request.body.read
#name= params['name']
#pin= params['pin']
#city=City.new(name:#name,pin:#pin)
if #city.save
return output(200, #city.as_json)
else
"cant create"
end
end
end
end
end
I am trying to post the below content through postman:
{
"name": "hamilton",
"pin":"222"
}
Please note that the route is configured and it does work when I visit the url. Also have ran db:migrate

How to get json response in rails api

I want to display all programmes which I got from a query as json response. I'm getting the programmes, but don't know how to render them through json. I'm using the jbuilder gem and created a create.json.buider.rb file. In my query I'm getting everything correctly, but I'm not receiving a JSON response with whatever details in I have in the query.
This is my controller. I have tried it like this but I'm not getting a json response. Only a status as 200.
class Api::V1::Categories::ProgrammesController < ApiController
respond_to :json
def category
#category=Category.all
#programmes=Programme.joins(:category).find_by(category_id: params[:category_id])
if #programmes.present?
render :json=> {:message=>"Programme not exists "}, :status=>422
else
render :json => #programmes
end
end
end
My create.json.jbuilder file:
json.programmes #programmes
I think you should change #programmes to { :programmers => #programmes.as_json }
class Api::V1::Categories::ProgrammesController < ApiController
def category
#category = Category.all
#programmes = Programme.joins(:category).find_by(category_id: params[:category_id])
if #programmes.present?
render :json=> {:message=>"Programme not exists "}, :status=>422
else
render :json => { :programmers => #programmes.as_json }
end
end
end

Unable to create correct Mongoid model from JSON

I'm trying to create a Mongoid model from the corresponding JSON structure.
However it does not include the embedded relation frameworks.
I'm using Mongoid 4.0. Am I doing something wrong or is this a bug?
If I store any embedded relation via store_as under a different name than the default serialization, it works as expected. Also, if I create the model in the database from JSON rather than initialize it everything's fine...
JSON input
{
"name": "MyName",
"frameworks": [
{
"name": "grails",
"runtime": "groovy",
"versions": []
}
]
}
Models
require 'mongoid'
class Vendor
include Mongoid::Document
include Mongoid::Attributes::Dynamic
# fields
field :name, type: String
# relations
embeds_many :frameworks
# validations
validates :name, presence: true
validates :frameworks, presence: true
end
class Framework
include Mongoid::Document
embedded_in :vendor
field :name, type: String
field :runtime, type: String
field :versions, type: Array
# validations
validates :name, presence: true
validates :runtime, presence: true
end
Test App
require 'json'
require 'require_relative'
require_relative 'vendor'
begin
json = JSON.parse(File.read('input.json'))
#profile = Vendor.new(json)
puts #profile.inspect
rescue JSON::ParserError => e
puts "Error: " << e.to_s
end

Mongo object's id as uploadings dirname in CarrierWave

I'm using Sinatra with Mongoid and CarrierWave. I need to store document's attachments in /public/attachments/DOCUMENTS_ID.
Model of Mongo document:
class Dcmnt
include Mongoid::Document
store_in collection: 'dcmnts'
field :published, type: Boolean
field :name, type: String
field :description, type: String
field :additional, type: String
field :created_at, type: Date
mount_uploader :attachment, Uploader, type: String
end
And action's code:
post '/admin/create' do
params.delete 'submit'
d = Dcmnt.new(
:published => params[:published],
:name => params[:name],
:description => params[:description],
:additional => params[:additional],
:created_at => Time.now
)
d.attachment = params[:photos]
d.save
end
When I'm setting up unloader like this:
class Uploader < CarrierWave::Uploader::Base
storage :file
def store_dir
'public/attachments/' + d.id
end
end
It doesn't works for some amzaing reason. Can you help me implement this feature?
Accessing models attributes in CarrierWave is provided via model key word
class Uploader < CarrierWave::Uploader::Base
storage :file
def store_dir
'attachments/' + model.id
end
end

Passing post data to other pages in Sinatra

This seems to work fine:
views/index.haml:
%form{:method => 'POST' :action => '/'}
%label{:for => 'name'} Name:
%input{:type => 'text, :value => #values[:name] || ""}
%input{:type => 'submit'}
app.rb:
post '/' do
#values = params
haml :review
end
views/review.rb
Hello #{params[:name]}!
However, when I try to send my post-data to the same view on a different URL I get an error, or in other words:
app.rb:
post '/' do
#values = params
redirect '/review'
end
get '/review' do
#values = params
haml :review
end
The data is not going through, but no error is raised.
How do I send the post-data across pages like this? Ideally, I do not want to create a database.
You can store the parameters in a session or specify the query string explicitly. Browser Redirect from Sinatra Documentation
As specified in the documentation, you may use sessions or convert the POST params to a query string and use it in the redirect method. A crude example would be:
Say the POST params hash inside the '/' block is:
{
:name => "Whatever",
:address => "Wherever"
}
This hash can be made into a string like so:
query = params.map{|key, value| "#{key}=#{value}"}.join("&")
# The "query" string now is: "name=Whatever&address=Wherever"
Now use this inside the post '/' do
redirect to("/review?#{query}")

Resources