Sinatra Beyond Basics : Processing a JSON Payload


To learn how to post a JSON payload and process it in the backend.


Step 1

This article will continue from where we left in the previous article Sinatra Beyond Basics : Post a JSON Payload. Let's organize. Create an app folder in the root of the project and move the controller directory into it. Rename the todo_spec.rb to todo_controller_spec.rb and move it to spec/app/controller folder.

Step 2

Change the test to post a JSON payload like this:

require 'spec_helper'
require_relative '../../../app/controller/todo_controller'
require 'json'

RSpec.describe TodoController do

  def app

  it 'should take a json payload' do
    body = { priority: 1, task: 'Get rich quick' }.to_json
    post "/", body, {'Content-Type' => 'application/json'}

    expect(last_response).to be_ok

Run the test:

$rspec spec/app/controller/todo_controller_spec.rb 

Step 3

The test does not force the code to do much. Let's change the test to add another expectation:

expect(last_response.body).to eq('{"success":"true"}')

Now the test fails.

Step 4

Change the todo_controller.rb:

require 'sinatra'
require 'json'

class TodoController < Sinatra::Base
  post '/' do   

    {success: 'true'}.to_json

Tip : You can use to check if the JSON syntax is valid.

Step 5

To read the posted JSON, you can do:

require 'sinatra'
require 'json'

class TodoController < Sinatra::Base
  post '/' do   
    input =
    puts input
    {success: 'true'}.to_json

If you copy the string printed as the value for input and use JSONLint, you will that it is a valid JSON.

    "priority": 1,
    "task": "Get rich quick"

Step 6

You can parse the posted JSON and convert it to a hash like this:

 payload = JSON.parse(body, :symbolize_names => true)

You can then process the data and persist it any type of data store in the backend.


In this article, we saw how to read the posted JSON from the client and convert it into a convenient hash for processing in the backend.

Related Articles

Create your own user feedback survey