user1883746
user1883746

Reputation:

How to introduce data in one API using RAILS

My question is basically the following. I am doing an API and for this goal I dont need a model.

It should be introduced some JSON data, do some operation and then present the result in JSON.

I have been watching many videos, tutorials and they always work with models. To test that the API works they use the command curl with some other options. SOmething like this:

curl \
-H "Accept: application/json" \
-H "Content-type: application/json" \
-X POST \
-d '{"title":"The Title will go here"}' \
http://localhost:3000/api/todo_lists.json

In this way they introduce another title in their model.

As I said, Im not interested in models and want to apply the same principle (curl command). But how can I send JSON data using the curl command and catching/reading this data in my API controller? Is it possible that someone shows me with a simple example how it would be? I am really new to Rails and Ruby.

Any help would be nice.

Upvotes: 0

Views: 75

Answers (4)

bpieck
bpieck

Reputation: 288

How about:

rails g controller todo_lists

Add to your routes:

resources :todo_lists, only: :create

And fill the controller with:

class TodoListsController < ActionController::Base
  def create
    @todo = JSON.parse(request.body, symbolize_names: true) 
      # this is a Hash - in your example your @todo would look like:
      # {title: "The Title will go here"}
    render json: process
      # render json: <some-hash> will create automatically the correct json from it
      # and set application/json Mimetype to response
  end

  private

  def process
    @response = <do_something with @todo>
    @response # should be a Hash
  end
end

But I think it's kind of strange to use post, if you do not change anything on your server - like saving some file, adding an entry to database or something similar. I think it fits more to conventions to use here GET (then just change everything above from create to index).

I think you only say, that you don't want to use models, because you do not need to save anything in database. That is absolutely ok. But if you want to do much logic in processing the json, you still should use some classes, that just do not inherit ActiveRecord::Base. The Big Models vs. Small Controllers paradigm really helps to keep code clean. So you could create some TodoListPorcessor class, initialize it with request.body and do the processing there, if it's more than five lines or similar.

Upvotes: 0

Carpela
Carpela

Reputation: 2195

I think you might be just trying to post stuff to your API rather than build it internally, in which case...

You might want to look at the HTTParty gem.

Then you can either send

HTTParty.post(your_url, 
 :body => { :something => 'something',
            :something_else => 12345 }.to_json, 
 :headers => { 'Content-Type' => 'application/json' } 
)

or just

httparty "http://yoursite.com"

from the command line.

Another alternative I found useful was the Postman app in chrome

Upvotes: 0

devkaoru
devkaoru

Reputation: 1162

in config/routes.rb make sure you have:

post 'api/todo_list' => 'todo_lists#create'

create a file app/controllers/todo_lists_controller.rb that has:

class TodoListsController < ApplicationController
  def create
    render :json => { message: "You posted a message with a title of #{params[:title]}" }, :status => 200
  end
end

skimming over this tutorial might be better suited for the problem you're having: https://www.airpair.com/ruby-on-rails/posts/building-a-restful-api-in-a-rails-application

Upvotes: 1

max
max

Reputation: 102144

There is nothing in Rails which forces you to use models. Rails is built around the model-view-controller paradigm but does not impose it on you. However if you are totally fresh at Rails or MVC I would recommend doing a basic tutorial so you actually understand the role of models before skirting them.

Creating an controller action which would just transform data is not different from creating an action which acts on models. Models just give us a good separation of concerns and a usable abstraction.

class ApiController < ActionController::Base
  # stupid example which reverses the passed title
  def filter
    data = filter_params.dup
    data[:title] = data[:title].reverse
    render json: data
  end

  private
  def filter_params
    # we whitelist the incoming parameters.
    params.permit(:title)
  end
end

Upvotes: 0

Related Questions