Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3396386
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T04:24:05+00:00 2026-05-18T04:24:05+00:00

I’m doing a proof of concept thing here and having a bit more trouble

  • 0

I’m doing a proof of concept thing here and having a bit more trouble than I thought I was going to. Here is what I want to do and how I am currently doing it.

I am sending my Sinatra app a json file which contains the simple message below.

[ 
        { 
        title: "A greeting!",
        message: "Hello from the Chairman of the Board" 
         }
]

From there I have a post which I am using to take the params and write them to sqlite database

post '/note' do

    data = JSON.parse(params) #<---EDIT - added, now gives error.

    @note = Note.new    :title => params[:title],
                          :message =>  params[:message],
                          :timestamp => (params[:timestamp] || Time.now) 
    @note.save
end

When I send the message the timestamp and the id are saved to the database however the title and message are nil.

What am I missing?

Thanks

Edit:

Now when I run my app and send it the json file I get this error:

C:/Users/Norm/ruby/Ruby192/lib/ruby/1.9.1/webrick/server.rb:183:in `block in start_thread’
TypeError: can’t convert Hash into String

Edit 2: Some success.

I have the above json in a file call test.json which is the way the json will be posted. In order to post the file I used HTTPClient:

require 'httpclient'

HTTPClient.post 'http://localhost:4567/note', [ :file => File.new('.\test.json') ]

After thinking about it some more, I thought posting the file was the problem so I tried sending it a different way. The example below worked once I changed n my post /note handle to this:

data = JSON.parse(request.body.read)

My new send.rb

require 'net/http'

require 'rubygems'
require 'json'

@host = 'localhost'
@port = '4567'

@post_ws = "/note"

@payload ={
    "title" => "A greeting from...",
    "message" => "... Sinatra!"
  }.to_json

def post
     req = Net::HTTP::Post.new(@post_ws, initheader = {'Content-Type' =>'application/json'})
          #req.basic_auth @user, @pass
          req.body = @payload
          response = Net::HTTP.new(@host, @port).start {|http| http.request(req) }
           puts "Response #{response.code} #{response.message}:
          #{response.body}"
        end

thepost = post
puts thepost

So I am getting closer. Thanks for all the help so far.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T04:24:06+00:00Added an answer on May 18, 2026 at 4:24 am

    Sinatra won’t parse the JSON automatically for you, but luckily parsing JSON is pretty straightforward:

    Start with requiring it as usual. require 'rubygems' if you’re not on Ruby 1.9+:

    >> require 'json' #=> true
    >> a_hash = {'a' => 1, 'b' => [0, 1]} #=> {"a"=>1, "b"=>[0, 1]}
    >> a_hash.to_json #=> "{"a":1,"b":[0,1]}"
    >> JSON.parse(a_hash.to_json) #=> {"a"=>1, "b"=>[0, 1]}
    

    That’s a roundtrip use to create, then parse some JSON. The IRB output shows the hash and embedded array were converted to JSON, then parsed back into the hash. You should be able to break that down for your nefarious needs.

    To get the fields we’ll break down the example above a bit more and pretend that we’ve received JSON from the remote side of your connection. So, the received_json below is the incoming data stream. Pass it to the JSON parser and you’ll get back a Ruby data hash. Access the hash as you would normally and you get the values:

    >> received_json = a_hash.to_json #=> "{"a":1,"b":[0,1]}"
    >> received_hash = JSON.parse(received_json) #=> {"a"=>1, "b"=>[0, 1]}
    >> received_hash['a'] #=> 1
    >> received_hash['b'] #=> [0, 1]
    

    The incoming JSON is probably a parameter in your params[] hash but I am not sure what key it would be hiding under, so you’ll need to figure that out. It might be called 'json' or 'data' but that’s app specific.

    Your database code looks ok, and must be working if you’re seeing some of the data written to it. It looks like you just need to retrieve the fields from the JSON.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want use html5's new tag to play a wav file (currently only supported
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am doing a simple coin flipping experiment for class that involves flipping a
I have a French site that I want to parse, but am running into
In my XML file chapters tag has more chapter tag.i need to display chapters
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.