50 Experience
0 Lessons Completed
0 Questions Solved


Posted in Ruby get GET request data

I'm making API endpoints and I'm having trouble getting the body of the request. Here is what I have:

In my routes I have set up an endpoint in my namespace:

namespace :api, :defaults => {:format => :json}  do
    resources :mainview do
        collection do
            get 'data'

In my front end I call the /api/mainview/data endpoint and pass it in a date inside of a json:

axios.get('/api/mainview/data', {"start_date":"2020-01-01"})
    .then(response => {
    .catch(error => {
      console.log("We are getting this error:")

And then, in my /api/mainview_controller.rb file, I got

module Api
    class MainviewController < AuthenticatedController
        def data
            puts "We are in the data function"
            #I want to print the body ({"start_date":"2020-01-01"}) of the request here 

So in this case, my API call seems to work as "We are in the data function" does get printed to the terminal. However, I don't know how to get the body or data from the request. If it's a post request I know I can use request.raw_post but I don't know what to use for GET requests. Most of the answers I found online are in PHP so any help would be great!

logo Created with Sketch.

Screencast tutorials to help you learn Ruby on Rails, Javascript, Hotwire, Turbo, Stimulus.js, PostgreSQL, MySQL, Ubuntu, and more. Icons by Icons8

© 2022 GoRails, LLC. All rights reserved.